如何在Backbone中使用RequireJs的命名空间

dev*_*pus 5 namespaces requirejs backbone.js

我不确定如何在模块化(RequireJs)Backbone环境中使用命名空间.

我已经想了一下它怎么可能看起来像,但我完全不能确定这是否是正确的方式.

app.js(由main.js执行)

define('App', ['underscore', 'backbone', 'Router'], function( _, Backbone, Router){
    function initialize(){
        var app = {}; // app is the global namespace variable, every module exists in app

        app.router = new Router(); // router gets registered

        Backbone.history.start();
    }

    return { initialize: initialize }
});
Run Code Online (Sandbox Code Playgroud)

messages.js

define('MessageModel', ['underscore', 'backbone', 'App'], function(_, Backbone, App){
    App.Message.Model; // registering the Message namespace with the Model class

    App.Message.Model = Backbone.Model.extend({
        // the backbone stuff
    });

    return App;
});
Run Code Online (Sandbox Code Playgroud)

这是正确的方法还是我完全错误的方式(如果是的话请纠正我!)

CD.*_*D.. 5

看看TODO Backbone + requireJs示例:

https://github.com/addyosmani/todomvc

  • 糟糕的回答,提供真实的内容 - 而不仅仅是外部链接 (9认同)