Ember.js 余烬数据1.0.0 Beta 2:规范化不再有效
我使用的是Ember Data 1.0.0 beta 1。我切换到beta 2(刚刚发布) 似乎模型序列化程序(我在其中规范化了id)不再工作了 我觉得normalize中的参数顺序从type,prop,hash改为type,hash,prop 这是《迁移指南》的建议:Ember.js 余烬数据1.0.0 Beta 2:规范化不再有效,ember.js,ember-data,Ember.js,Ember Data,我使用的是Ember Data 1.0.0 beta 1。我切换到beta 2(刚刚发布) 似乎模型序列化程序(我在其中规范化了id)不再工作了 我觉得normalize中的参数顺序从type,prop,hash改为type,hash,prop 这是《迁移指南》的建议: normalize: function (type, property, hash) { // normalize the `_id` var json = { id: hash._id };
normalize: function (type, property, hash) {
// normalize the `_id`
var json = { id: hash._id };
delete hash._id;
// normalize the underscored properties
for (var prop in hash) {
json[prop.camelize()] = hash[prop];
}
// delegate to any type-specific normalizations
return this._super(type, property, json);
}
beta 2中的参数顺序现在是(类型、哈希、属性)。因此,标准化模型在beta 2版本中不包含id
如果我将参数切换为type、hash、property,则id将被填充,但此时所有其他属性都将变为ampty
因此,看起来您不能再使用normalize来规范化id和任何带下划线的属性 过渡文档中有一个修订版的normalize函数(稍微健壮一些)
如果您只在“代码”>规范化:函数(…<代码/ >)上切换订单,您将得到空白参数。您还必须在“代码”上切换它。返回超级。(…<代码> >行> < /P> < P> >在转换文档中有归一化函数的修订版本(稍微强一些)。< /P>
<>你将得到空白的参数,如果你只需在代码中用“代码>标准化:函数”(…<代码> >切换。(…line.修订版确实很好用。因为我只需要规范化Id,所以我现在用的是normalizeId函数。thx!修订版确实很好用。因为我只需要规范化Id,所以我现在用的是normalizeId函数。thx!
App.ApplicationSerializer = DS.RESTSerializer.extend({
normalize: function(type, hash, property) {
var normalized = {}, normalizedProp;
for (var prop in hash) {
if (prop.substr(-3) === '_id') {
// belongsTo relationships
normalizedProp = prop.slice(0, -3);
} else if (prop.substr(-4) === '_ids') {
// hasMany relationship
normalizedProp = Ember.String.pluralize(prop.slice(0, -4));
} else {
// regualarAttribute
normalizedProp = prop;
}
normalizedProp = Ember.String.camelize(normalizedProp);
normalized[normalizedProp] = hash[prop];
}
return this._super(type, normalized, property);
}
});