nny*_*yby 4 ember.js ember-data
有没有一种常规方法可以在Ember Data中查找单例?我已GameState根据这些说明(在"Singular"下)为我的模型设置了适配器:https://stackoverflow.com/a/23974804/173630
现在,当我获取GameState时,我希望能够这样做:
store.find('gameState');
Run Code Online (Sandbox Code Playgroud)
但是,它会findAll在Ember Data 中启动一个请求类型,它需要一个数组,而不是一个对象.
我的另一个选择是做这样的事情:
store.find('gameState', 'mine');
Run Code Online (Sandbox Code Playgroud)
该mine参数被忽略,但这将成功获取并加载我的GameState.这里的缺点是Ember Data GameState在我的商店中创建了一个id为的额外实例mine.有什么方法可以防止这种情况吗?
我正在使用Ember Data 1.0.0-beta.11.
这是我的模型的样子:
var GameState = DS.Model.extend({
currentPuzzle: DS.belongsTo('puzzle'),
currentRound: DS.belongsTo('puzzleRound'),
currentInventory: DS.attr('number'),
moves: DS.hasMany('move', {async: true}),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
});
Run Code Online (Sandbox Code Playgroud)
不,不幸的是,这样做会产生副作用.你可以在这里做一些不同的事情:
return this.store.find('foo').then(function(records){
return records.get('firstObject');
});
Run Code Online (Sandbox Code Playgroud)
var store = this.store;
return $.ajax('foo').then(function(data){
return store.push('foo', data); // push returns the record
});
Run Code Online (Sandbox Code Playgroud)
http://emberjs.com/api/data/classes/DS.Store.html#method_push