[英]Ember.js: How to resolve an async hasMany relationship within Ember.Route?
How do i resolve an async hasMany relationship within Ember.Route? 如何解决Ember.Route中的异步hasMany关系?
To be specific: I can't figure out why this.modelFor('collection').get('id')
returns an ID but this.modelFor('collection').get('recipes_refs')
an object (maybe a promise?). 具体来说:我不知道为什么
this.modelFor('collection').get('id')
返回ID,但是this.modelFor('collection').get('recipes_refs')
是一个对象(也许是诺言?)。
I have to resolve recipes_refs
because i need to figure out if the currently viewed recipe collection contains the currently requested recipe. 我必须解析
recipes_refs
因为我需要弄清楚当前查看的食谱集合是否包含当前请求的食谱。
// Route
App.CollectionRecipeRoute = Ember.Route.extend({
model: function () {
console.log(this.modelFor('collection').get('id'))
}
});
// Models
App.Collection = DS.Model.extend({
title: DS.attr('string'),
assets: DS.attr(),
status: DS.attr('string'),
recipes_refs: DS.hasMany('recipe', { async: true })
});
App.Recipe = DS.Model.extend({
title: DS.attr('string'),
ingredients: DS.attr('string')
});
You're right that this.modelFor('collection').get('recipes_refs')
is returning a promise. 您是对的,
this.modelFor('collection').get('recipes_refs')
返回了诺言。 You can deal with it just like any other promise by using then
to handle the cases where it resolves or rejects. 通过使用
then
处理解决或拒绝的情况,您可以像处理任何其他promise一样处理它。
this.modelFor('collection').get('recipes_refs').then(function(recipes){
// At this point 'recipes' is a collection of live objects
recipes.forEach(function(recipe,i){
// do something with each recipe in the collection
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.