模型获取行为(特别是设置模型 ID)在 1.1.0 和 1.1.2 之间是否发生了变化?
我检查了更新日志,但找不到任何相关内容。
以下不再有效:
var Wibble = Backbone.Model.extend({
urlRoot: 'rest/wibble',
idAttribute: 'wibbleId'
});
var model = new Wibble();
model.id = 1;
model.fetch()
它请求/rest/wibble 而不是以前的/rest/wibble/1。
例子:我使用 url() 而不是 fetch() 来演示
最佳答案
当模型不是新模型时,模型通过附加 /[id]
来构建其 url:
url: function() {
var base = _.result(this, 'urlRoot') ||
_.result(this.collection, 'url') ||
urlError();
if (this.isNew()) return base;
return base.replace(/([^\/])$/, '$1/') + encodeURIComponent(this.id);
}
但似乎 model.isNew
在 1.1.0 和 1.1.2 之间发生了变化
-
isNew: function() { return this.id == null; },
-
isNew: function() { return !this.has(this.idAttribute); },
检查现在只考虑 idAttribute
描述的属性,不再考虑 id
属性。
像在 1.1.2 示例中那样设置 idAttribute
可能是最安全的选择:
model.set('wibbleId', 123);
关于backbone.js - backbone fetch id 改变了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26145165/