Backbone.js Router initialization doesn't run immediately

不打扰是莪最后的温柔 提交于 2019-12-01 21:21:05

Indeed the problem here is with initialize returning before the ajax call inside it has been resolved.

What you can do is do something like the following in your entry point (typically $.ready())

var self = this,
    p = $.ajax({
    url: 'data/todolist.json',
    dataType: 'json'
});

p.done(function (data) {
    AppRouter = new Backbone.Router({data: data});
    Backbone.history.start({ pushState: true });    
});

This will fetch the routes, and then initialize the router with them as well as start Backbone.history. Obviously you don't need to do the ajax call again in initialize, just use the data passed in options.

It looks like this happens because this._index is only set within the ajax callback. Because this is asynchronous, there is no guarantee that it will have executed before the index event handler triggers.

According to the docs, the models you need on initial load should be bootstrapped.

If that's not possible, one way to structure this code might be to fetch your data when the route is hit, and bind a reset event to your view, e.g.

var CategoriesView = Backbone.View.extend({
    initialize: function() {
        this.collection.on("reset", this.render);

    },

    ...

var AppRouter = Backbone.Router.extend({

    _index: null,
    _todos: null,

    routes: {
        "": "index",
        "category/:name": "hashcategory"  
    },

    initialize: function(options){
        var self = this;

        self._todos = new TodosCollection();
        self._index = new CategoriesView({ collection: self._todos });
    },

    index: function(){
        this._todos.fetch();
    },

You'd also need to setup your collection to construct an appropriate URL to request data/todolist.json.

Your problem is the order of your routes. Routes are evaluated in order so the "*action" will ALWAYS match.

Try:

routes: {
  "category/:name": "hashcategory"
  "*action": "index",
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!