我想知道为什么尝试呈现索引函数给我一个cannot call method render of null
错误。有没有办法让索引函数等待呈现,直到路由器的初始化完成。 console.logs似乎暗示索引函数试图在初始化完成之前呈现ajax调用延迟索引的呈现,直到初始化完成
1.Uncaught TypeError: Cannot call method 'render' of null gallery.js:231
2. XHR finished loading: "http://localhost:3000/readjsonfile". jquery.js:8241
3.success
这是代码。我有
var Gallery = Backbone.Router.extend({
routes: {
"": "index",
},
据initally设置为null
_index: null,
图库路由器检查的初始化如果该索引是空的路线,并且,如果是,创建一个具有数据的新观点。
initialize: function(options) {
var ws = this;
if (this._index === null){
$.ajax({
url: 'galleries',
dataType: 'json',
data: {},
success: function(data) {
console.log("success");
console.log(data);
ws._data = data;
ws._photos = new PhotoCollection(data);
ws._index = new IndexView({model: ws._photos});
console.log(ws._index);
Backbone.history.loadUrl();
},
error: function(r){
console.log("error");
console.log(r);
}
});
return this;
}
return this;
},
这是该指数函数,初始化后立即放置,呈现在初始化创建上面的观点,但是,我发现了一个Cannot call method 'render' of null
错误
index: function() {
this._index.render();
},
这是一个真棒答案。我会去玩弄它,以确保我能够把它运用起来并理解这一切,然后再回来接受。谢谢。 – BrainLikeADullPencil