我很困惑,可以使用一些帮助。了解backbone.js属性和toJSON()
我渲染函数中,我有以下三个调试行:
console.debug(this.model);
foo = this.model.toJSON();
console.debug(foo);
在第一线的输出是从服务器获取数据模型实例和属性,属性是人口稠密与我所期望的一样。
但是,第二个console.debug调用包含一个空对象。
什么给?不应该这第二个调试输出位包含相同的属性,但JSONified?
下面的代码完成的位:
function get_race() {
var RaceModel = Backbone.Model.extend({
urlRoot: api_root + 'race/1/?format=json',
});
var RaceView = Backbone.View.extend({
template: _.template('<h1>a template</h1><h2>desc: <%= year %></h2>'),
initialize: function() {
this.model = new RaceModel();
this.model.fetch();
this.render();
},
render: function() {
console.debug(this.model);
foo = this.model.toJSON();
console.debug(foo);
this.$el.html(this.template(this.model));
return this;
}
});
var race_view = new RaceView({ el: $("#backbone_test") });
你使用什么浏览器?我曾经在Firefox/Firebug中做过这样的事情,并且在这个用法中看到问题 – 2013-02-24 02:57:39
toJSON中的模型应该返回代表模型实例上属性属性的对象 - 它不会与模型完全相同,因为模型有其他的属性和方法,所以没有看到属性,我不能说你应该看到什么具体的 – kinakuta 2013-02-24 03:01:31
一件事添加 - 不要传递this.model到你的模板函数,通过this.model.attributes – kinakuta 2013-02-24 03:19:50