私は混乱していて、助けを借りることができます。
私はレンダリング関数の中にいて、次の3つのデバッグ行があります。
console.debug(this.model);
foo = this.model.toJSON();
console.debug(foo);
最初の行の出力は、サーバーからフェッチされたデータを含むモデルインスタンスであり、attributesプロパティには私が期待するものが入力されています。
ただし、2番目のconsole.debug呼び出しには空のオブジェクトが含まれています。
何が得られますか?デバッグ出力のこの2番目のビットには、同じ属性が含まれている必要がありますが、JSON化されていますか?
以下はコードの完全なビットです:
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") });