我尝试为 django +bone.js 创建 rest-api,但我遇到了麻烦,model.fetch() 不会更新默认模型值。我在哪里犯错了?
骨干.js 模型
window.User = Backbone.Model.extend({
urlRoot: '/user/',
initialize: function() {
console.log("init Model User");
}
defaults: {
name: "Default title",
pass: 2011,
}
});
主干.js 视图
window.UserView = Backbone.View.extend({
initialize:function () {
console.log('Initializing User View');
this.model.bind('change', this.render, this);
this.model.fetch ({
success: function () {
alert('success');
}
});
console.log(this.model);
this.render();
},
render:function () {
console.log('Print tpl UserView');
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
创建视图
var userView = new UserView({model: new User()});
django 响应 [{"name": "LOLOLO", "pass": "12345"}]
控制台.log(this.model)
打印
attributes: Object
0: Object
name: "LOLOLO"
pass: "12345"
__proto__: Object
name: "Default title"
pass: 2011