Backbone.js:separate the view ,collection,model to different js file,they could't recognize each other

后端 未结 2 458
夕颜
夕颜 2021-02-04 20:21

I use Backbone.js to create a web app,all the view,collection and model write into one js file,it success!

now I want separate them to different js files,just like:

2条回答
  •  我在风中等你
    2021-02-04 20:40

    Once you add the function wrappers:

    $(function() {
        // ...
    })
    

    You've introduced new scopes and all the vars declared inside those functions are only visible within those functions. You can get around this by making them global (i.e. properties of window):

    $(function(){
        window.manageModel = Backbone.Model.extend({
            //...
        });
    });
    

    or better, introduce an application namespace:

    $(function(){
        window.app = window.app || { };
        window.app.manageModel = Backbone.Model.extend({
            //...
        });
    });
    

    and then refer to things through app like app.manageModel:

    $(function(){
        window.app = window.app || { };
        window.app.someCollection = Backbone.Collection.extend({
            model: app.manageModel,
            //...
        });
    });
    

提交回复
热议问题