[英]Looping over an Ember.js promise
I am trying to loop over what I believe to be an Ember promise, but all I can seem to get returned is an object, when it should be an array. 我试图循环我认为是Ember的承诺,但我似乎得到的只是一个对象,它应该是一个数组。
jsbin: http://emberjs.jsbin.com/qakine/1/edit jsbin: http ://emberjs.jsbin.com/qakine/1/edit
If I just loop over the items in the template then I have no issues, but I want to be able to interact with the array items in my controller. 如果我只是循环模板中的项目,那么我没有问题,但我希望能够与我的控制器中的数组项进行交互。 Any help appreciated.
任何帮助赞赏。
Quite a few things here, let me see if I can remember them all: 这里有很多东西,让我看看我是否能记住它们:
When you have a route under a resource, the routes and controllers of that route should take their parent's name. 当您在资源下有路由时,该路由的路由和控制器应采用其父级的名称。
App.Router.map(function() {
this.resource("parent", function(){
this.route("child");
});
});
App.ParentChildRoute...
App.ParentChildController...
Handlebars can't access normal functions from your controller, this should be converted into a computed property. 把手无法从您的控制器访问正常功能,这应该转换为计算属性。
App.ParentChildController = Ember.ObjectController.extend({
middleNames: function(){
...
}.property('middles.[]')
});
naming is case sensitive 命名区分大小写
{{#each name in middlenames}}
should be 应该
{{#each name in middleNames}}
Example: http://emberjs.jsbin.com/cowibi/1/edit 示例: http : //emberjs.jsbin.com/cowibi/1/edit
I created an new example based on yours to make things clear. 我创建了一个基于你的新例子,以便清楚地表达出来。 You can check it out here:
你可以在这里查看:
http://emberjs.jsbin.com/hokabe/4/edit http://emberjs.jsbin.com/hokabe/4/edit
For Ember route's model
hook, if the return value is a promise, the route will wait for the promise resolved and pass the resolved value to controller's model
property. 对于Ember route的
model
钩子,如果返回值是promise,则路由将等待promise的解析并将解析后的值传递给controller的model
属性。
And a App.Parent
instance's middles
property returns a promise (actually it's a DS.PromiseArray
instance) which will resolve a middles array (actually it's a DS.ManyArray
instance). 和
App.Parent
实例的middles
属性返回一个承诺(实际上它是一个DS.PromiseArray
实例),这将解决中段阵列(实际上它是一个DS.ManyArray
实例)。
so for getting children you can simply do this: 所以为了生孩子,你可以简单地这样做:
App.ParentMiddlesRoute = Ember.Route.extend({
model: function() {
return this.modelFor('parent').get('middles');
}
});
Note that modelFor
argument is a route name but not a model name. 请注意,
modelFor
参数是路径名称,但不是模型名称。 It means "Get the model from given route". 这意味着“从给定路线获取模型”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.