使用 fetch 方法,我从本地 json 获取模型,但通常成功,没有什么能安慰我..
我的代码有任何问题..我调用渲染函数的方式在成功时是正确的吗?
我使用的 JSON:
[
{'name':'student1'},
{'name':'student2'},
{'name':'student3'}
];
我的代码:
$(document).ready(function(){
var school = {};
school.model = Backbone.Model.extend({
defaults:{
name:'name yet to decide'
}
});
school.collect = Backbone.Collection.extend({
model:school.model,
url:'js/school.json',
initialize:function(){
console.log(this.collection)//shows the undefined
}
});
school.view = Backbone.View.extend({
initialize:function(){
var that = this;
this.collection = new school.collect;
this.collection.fetch({
success:function(model,response){
console.log(model,response);//i am not get any mode or response here
that.render(); //not at all calling..
}
});
},
render:function(){
console.log('hi')
}
});
var newModel = new school.model;
var newSchool = new school.view({model:newModel});
})