Search code examples
backbone.jsrequirejsstackmob

Stackmob with requires.js


I'm laying out the basic structure and just started to create models. The issue is that the reference to the initialized Stackmob is lost within the model.

main.js
require(['modernizr','jquery','backbone', 'underscore', 'routers/router', 'stackmob'], function(Modernizr, $, Backbone, _, Router, Stackmob) {

    StackMob.init({
        appName: {appName},
        clientSubdomain: {clientSubdomain},
        apiVersion: 0
    });

    // Instantiates a new Router
    this.router = new Router();
});

Model.js
define(['jquery', 'backbone', 'stackmob', 'models/business'], function($, Backbone, Stackmob, Business) {

    var BusinessesCollection = StackMob.Collection.extend({

            model: Business,

            // Model Constructor
            initialize: function() {

            }

    });

    // Returns the Model class
    return BusinessesCollection;

});

How can I get the initialized StackMob object to be accessible to the model module?


Solution

  • Introduce a sort of instance of StackMob?

    my-stackmob.js

    define(['stackmob'], function(StackMob) {
        StackMob.init({
            appName: {appName},
            clientSubdomain: {clientSubdomain},
            apiVersion: 0
        });
        // return a particular StackMob that we've initialised
        return StackMob;
    });
    

    main.js

    (would not appear to use StackMob directly any more, based on your example code)

    require(['modernizr','jquery','backbone', 'underscore', 'routers/router'], function(Modernizr, $, Backbone, _, Router) {
        // Instantiates a new Router
        this.router = new Router();
    });
    

    Model.js

    (uses my-stackmob)

    define(['jquery', 'backbone', 'my-stackmob', 'models/business'], function($, Backbone, Stackmob, Business) {
        var BusinessesCollection = StackMob.Collection.extend({
    
                model: Business,
    
                // Model Constructor
                initialize: function() {
    
                }
    
        });
    
        // Returns the Model class
        return BusinessesCollection;
    });