IDを事前入力したサーバーに新しいモデルを追加するときに、.save()が不正なisNew()値を報告する問題が発生しました。私はそれを止めて、model.idを手動で設定しました。私はちょうど罰金collection.getByCid(CID)を経由して、そうすることができる午前かかわらず、私は、collection.get(id)を使用して、任意のレコードを取得することができません。この時点でbackbone.js collection.get(id)returned undefined
RecordListItemView = Backbone.View.extend({
initialize:function() {
var record = this.model.attributes;
if (record.user_id) {
this.model.id = parseInt(record.user_id);
record.id = parseInt(record.user_id);
// Added this so ID would show up in model and .attributes
}
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render:function (eventName) {
$(this.el).html(this.template(this.model));
console.log(this);
return this;
}
});
:
は、ここに私の見解です。ここで
は私にconsole.log出力です:
d
$el: e.fn.e.init[1]
cid: "view36"
el: HTMLLIElement
model: d
_callbacks: Object
_escapedAttributes: Object
_pending: Object
_previousAttributes: Object
_silent: Object
attributes: Object
id: 15
user_id: "15"
user_name: "Test"
__proto__: Object
changed: Object
cid: "c8"
collection: d
id: 15
__proto__: x
options: Object
__proto__: x
は、idフィールドを含めるために、私のデータベースを変更することなくcollection.get(ID)を固定する方法はありますか?ベンジャミン・コックスによって以下掲示よう
(現在PKとしてUSER_ID使用):(不要として除去のparseInt()を用いて)
は交換
this.model.id = record.user_id;
this.model.set({ id: record.user_id });
と..コレクションのinternal_byId []配列を更新するモデルの変更イベントをバイパスすることを避けるためです。
は両方をテストした後、私はムーを使用して巻き取るのが短すぎparse
提案..です
parse: function(response) {
return {
id: response.user_id,
user_id: response.user_id,
user_name: response.user_name
};
}
サーバーからフェッチするときに 'user_id'を' id'にコピーするための['parse'](http://documentcloud.github.com/backbone/#Model-parse)実装を追加することも良い考えかもしれません。 –
ベンジャミン - それはかなり意味がある、私は明日オフィスに入るとすぐにそれをテストするだろう。 Mu - そのために 'parse'を調べなければなりませんが、データをハッキングするよりも、バックボーンの方法を使う方がはるかに良いでしょう。あなたがた両方に感謝します。 – Rosseyn
あなたの提案にお礼をいただき、ありがとうございました。 – Rosseyn