简体   繁体   中英

Where do I put the code to bootstrap a collection into backbone.js?

I am new to javascript and I saw another post with a similar question but I'm not sure how to actually inject models into a backbone.js that lives in a separate file.

In my index file I have the following which is starting the app:

$(function () {
    var app = new App();        
    Backbone.history.start();
});  

Inside my application.js file I have the router which needs to have customers:

window.App = Backbone.Router.extend({     

    routes: {
        "": "home"
    },

    home: function () {
        console.log("route::home");
        console.log(this.customers);
    }
});

How can I actually get this.customers to be injected into the application? Where does this code live?

I wrote an article that outlines how code like what you've shown here is potentially an anti-pattern, and illustrates a simple way of getting the data bootstrapped. Though your direct question is not the purpose of this article, I think the contents of this article should lead you in a direction that does solve the problem your running in to.

http://lostechies.com/derickbailey/2011/08/30/dont-limit-your-backbone-apps-to-backbone-constructs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM