我遇到了這個確切的問題,試圖張貼到不可變的屬性。 Backbone模型的問題是,默認情況下,它們發佈全部或全部。但你可以做部分更新。爲了解決這個問題,我創建了一個Backbone.Model後裔,推翻像這樣的model.save:
save : function(key, value, options) {
var attributes, opts;
//Need to use the same conditional that Backbone is using
//in its default save so that attributes and options
//are properly passed on to the prototype
if (_.isObject(key) || key == null) {
attributes = key;
opts = value;
} else {
attributes = {};
attributes[key] = value;
opts = options;
}
//Now check to see if a partial update was requested
//If so, then copy the passed attributes into options.data.
//This will be passed through to Backbone.sync. When sync
//sees that there's an options.data member, it'll use it instead of
//the standard attributes hash.
if (opts && opts.partialUpdate) {
opts["data"] = JSON.stringify(attributes);
opts["contentType"] = "application/json";
}
//Finally, make a call to the default save now that we've
//got all the details worked out.
return Backbone.Model.prototype.save.call(this, attributes, opts);
}
這讓我我想要的屬性,選擇性地發佈到後臺,像這樣:
//from the view - the GET may have delivered 20 fields to me, but I'm only interested
//in posting the two fields.
this.model.save({
field1 : field1Value,
field2 : field2Value
},{
partialUpdate : true
});
不能告訴你這是怎麼讓我的生活變得如此簡單!現在,有人可能會問,爲什麼不通過changedAttributes()JSON?原因是因爲在某些情況下,已更改的屬性僅適用於客戶端,特別是要引用對也使用該模型的視圖的更改。
在任何情況下,嘗試了這一點...
應該有路過'created_at'回服務器沒問題,只要你的Rails應用程序是妥善書寫和保護。你的Rails開發是錯誤的。 – meagar