如何让模板等到路由加载数据控制器取决于?
How to make template wait until route loads data controller depends on?
在模板中我有一个字段:
{{#if thisIsIt}}Hello{{/if}}
在控制器中:
thisIsIt: function()
{
return this.get("underdata").get("length") // 1
}.property('underdata')
在路由器中:
setupController: function(controller, model)
{
this.store.find('underdata', {id: 1 }).then(function(underdata) // 2
{
controller.set("underdata", underdata); // 3
});
}
调用顺序是:2,1,3。因此,满足模板的控制器会尝试获取一定长度的 underdata。但是 underdata 是未定义的,因为 3 还没有被调用。抛出错误 3 后最终被调用。
如何让controller等到setupController从underdata中获取必要的数据?
你的问题是承诺。虽然先调用 setupController
函数,但稍后解析承诺。
解决方法是使用model
钩子加载数据。如果 model
挂钩确实不起作用,您也可以使用 beforeModel
或 afterModel
挂钩。
这三个钩子将等待返回的承诺在继续路由之前解决。
在模板中我有一个字段:
{{#if thisIsIt}}Hello{{/if}}
在控制器中:
thisIsIt: function()
{
return this.get("underdata").get("length") // 1
}.property('underdata')
在路由器中:
setupController: function(controller, model)
{
this.store.find('underdata', {id: 1 }).then(function(underdata) // 2
{
controller.set("underdata", underdata); // 3
});
}
调用顺序是:2,1,3。因此,满足模板的控制器会尝试获取一定长度的 underdata。但是 underdata 是未定义的,因为 3 还没有被调用。抛出错误 3 后最终被调用。
如何让controller等到setupController从underdata中获取必要的数据?
你的问题是承诺。虽然先调用 setupController
函数,但稍后解析承诺。
解决方法是使用model
钩子加载数据。如果 model
挂钩确实不起作用,您也可以使用 beforeModel
或 afterModel
挂钩。
这三个钩子将等待返回的承诺在继续路由之前解决。