如何使用字符串作为主键与Ember Data? [英] How to use a string as a primary key with Ember Data?
本文介绍了如何使用字符串作为主键与Ember Data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
Pult.Zone = DS.Model.extend({
name: DS.attr('string'),
authoritative: DS.attr('boolean'),
user_id: DS.attr('number'),
rulesets: DS.hasMany('Pult.Ruleset')
});
Pult.RESTAdapter.map('Pult.Zone', {
primaryKey: 'name',
rulesets: { key: 'rulesetIds' }
});
但是,看起来似乎没有正确的选择主键。我已经提交了所有区域的列表。
However, it doesn't seem like is picking up on the primary key correctly. I have rendered a list of all zones.
这是一个测试用例:
zones = Pult.store.findAll(Pult.Zone);
zones.get('length'); // Returns 10
zones = Pult.store.findAll(Pult.Zone);
zones.get('length'); // Returns 20
所以每次从服务器加载区域时,都会将它们添加到本地列表中,因为它不认为它们已经存在。任何方法来解决这个问题,还是要尝试模拟一些代理键?
So every time I load zones from the server, it adds them to the local list, since it does not recognize them as already existing. Any way to fix this, or will I have to try to mock up some surrogate keys?
推荐答案
升级到 Ember Data 1.0.0 Beta 2 ,我发现一个解决方案:
After upgrading to Ember Data 1.0.0 Beta 2, I found a solution that works:
App.Zone = DS.Model.extend({
name: DS.attr('string'),
user_id: DS.attr('number'),
});
App.ZoneSerializer = DS.RESTSerializer.extend({
normalize: function(type, hash, property) {
// Ember Data use the zone name as the ID.
hash.id = hash.name;
// Delegate to any type-specific normalizations.
return this._super(type, hash, property);
}
});
这篇关于如何使用字符串作为主键与Ember Data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文