Stackmob with requires.js

时间:2012-06-20 06:01:43

标签: backbone.js requirejs stackmob

我正在布局基本结构,并开始创建模型。问题是在模型中丢失了对初始化Stackmob的引用。

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;

});

如何让初始化的StackMob对象可供模型模块访问?

1 个答案:

答案 0 :(得分:5)

介绍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

(根据您的示例代码,似乎不再使用StackMob)

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

Model.js

(使用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;
});