【发布时间】:2012-05-07 23:04:09
【问题描述】:
我的代码如下:
var AppRouter = Backbone.Router.extend({
_data: null,
_length: 0,
_index: null,
_todos: null,
_subtodolist: null,
_subtodos: null,
routes: {
"*action": "index",
"category/:name": "hashcategory"
},
initialize: function(options){
var self = this;
if (this._index === null){
$.ajax({
url: 'data/todolist.json',
dataType: 'json',
data: {},
success: function(data) {
self._data = data;
self._todos = new TodosCollection(data);
self._index = new CategoriesView({collection: self._todos});
//self._index.render();
}
});
return this;
}
return this;
},
index: function(){
this._index.render();
},
....
但是当我开始时,firebug 控制台面板总是告诉我this._index 在index 函数中为空。我必须在$.ajax success 回调函数的底部使用self._index.render() 来渲染主页(上面已注释掉)。 index 函数似乎在 initialize 函数之前运行。这怎么会发生,我该如何解决?
顺便说一句,在routes中,如果我使用"": "index",它将不起作用。我必须使用"*action": "index"。但我在其他地方了解到默认 url 可能只是空字符串。为什么我不能在这里使用它?
【问题讨论】:
标签: backbone.js backbone-routing