我正在尝试实现一个需要另一个(CampaignsNew
Needing AppsIndex
)的控制器,该控制器看起来像
App.CampaignsNewController = Ember.Controller.extend({
needs: ['appsIndex']
});
在我的CampaignsNew
模板中,我通过
{{#if controllers.appsIndex.content.isUpdating}}
{{view App.SpinnerView}}
{{else}}
{{#each controllers.appsIndex.content}}
{{name}}
{{/each}}
{{/if}}
但是controllers.appsIndex.content.isUpdating
,从来都不是真的。即,它试图在加载数据之前显示数据。
我的AppsIndex
路线已覆盖该模型:
App.AppsIndexRoute = Ember.Route.extend({
model: function(controller) {
var store = this.get('store').findAll('app');
}
...
});
如果将相同的代码放入CampaignsNew
路由并将模板修改为each
through,则可以使它正常工作controller.content
。哪个对我needs
说不使用路线?如果我转到该/apps
页面并加载数据,然后导航到该/campaigns/new
页面,它也将起作用。
我该如何工作?谢谢!
编辑:根据要求,我路由器的相关部分:
App.Router.map(function() {
this.resource('apps', function() {
...
});
this.resource('campaigns', function() {
this.route('new');
});
});
并且AppsIndex
在/apps
和CampaignsNew
在访问/campaigns/new
Edit2:在通过@ kingpin2k实施建议之后,我发现Ember抛出了错误。以下是更新的文件和收到的错误。
App.CampaignsNewController = Ember.ObjectController.extend({
pageTitle: 'New Campaign'
});
App.CampaignsNewRoute = Ember.Route.extend({
model: function(controller) {
return Ember.RSVP.hash({
campaign: this.store.createRecord('campaign'),
apps: this.store.find('app')
});
// return this.store.createRecord('campaign');
},
setupController: function(controller, model){
controller.set('apps', model.apps);
this._super(controller, model.campaign);
}
});
灰烬引发此错误:
Error while loading route: Error: Assertion Failed: Cannot delegate set('apps', <DS.RecordArray:ember689>) to the 'content' property of object proxy <App.CampaignsNewController:ember756>: its 'content' is undefined.
我在网上阅读这是因为内容对象不存在。如果我这样设置:
App.CampaignsNewController = Ember.ObjectController.extend({
content: Ember.Object.create(),
...
});
然后,该页面将正确加载,并且在检查Ember Chrome扩展程序时,可以看到数据已加载。但它不会显示在页面上。我想发生这种情况是因为content
对象存在,所以Ember在渲染模板之前没有等待模型的承诺完成。似乎有些奇怪,尽管您必须以这种方式定义内容。有关如何处理此问题的任何见解?
Edit3:在另一个线程中为我回答的问题
根据您的路由器,apps
不是的父项campaigns/new
。
这意味着有人可能会打击#/campaigns/new
和灰烬会打ApplicationRoute
,CampaignsRoute
和CampaignsNewRoute
填充所请求的URL的必要信息。使用需求作为控制器之间进行交流的一种方式实际上仅在祖传模式中才有意义(也就是与父母,祖父母等交流)。
就像另一条快速注释一样,它AppsIndex
是的路由Apps
,当您的网址中包含孩子时不会被点击。例如
this.resource('apps', function() {
this.resource('chocolate', function(){
.....
});
});
#/apps/chocolate
ApplicationRoute
AppsRoute
ChocolateRoute
ChocolateIndexRoute
仅当您未指定资源的路线,并且您正在点击该确切的资源(也就是该资源之后的任何内容)时,才会命中索引路由。
您可以从特定的挂钩返回多个模型:
App.FooRoute = Em.Route.extend({
model: function(){
return Em.RSVP.hash({
cows: this.store.find('cows'),
dogs: this.store.find('dogs')
});
}
});
如果您希望主要模型仍然是母牛,则可以在setupController
级别上将其切换。
App.FooRoute = Em.Route.extend({
model: function(){
return Em.RSVP.hash({
cows: this.store.find('cows'),
dogs: this.store.find('dogs')
});
},
setupController: function(controller, model){
controller.set('dogs', model.dogs); // there is a property on the controller called dogs with the dogs
this._super(controller, model.cows); // the model backing the controller is cows
}
});
在这里查看第二个答案EmberJS:如何在同一路线上加载多个模型?(第一个也是正确的,只是没有提到从模型钩子返回多个模型的陷阱)。
您也可以只在期间设置该属性setupController
,尽管这意味着该属性在页面加载后将不可用,但稍后将不可用。
如果您不打算使用模型来支持控制器,请使用Controller。
App.FooRoute = Em.Route.extend({
model: function(){
return undefined;
}
});
如果要将控制器的模型设置为某种对象,请使用ObjectController,这不是一个集合。
App.FooRoute = Em.Route.extend({
model: function(){
return Em.RSVP.hash({
cows: this.store.find('cows'),
dogs: this.store.find('dogs')
});
}
});
如果某些东西将成为某种形式的集合,请使用ArrayController。
App.FooRoute = Em.Route.extend({
model: function(){
return ['asdf','fdsasfd'];
}
});
如果您覆盖setupController,则除非您明确告知它或使用,否则它将不会设置控制器的模型this._super
。
App.FooRoute = Em.Route.extend({
model: function(){
return Em.RSVP.hash({
cows: this.store.find('cows'),
dogs: this.store.find('dogs')
});
},
setupController: function(controller, model){
controller.set('cows', model.cows);
controller.set('dogs', model.dogs);
// uh oh, model isn't set on the controller, it should just be Controller
// or you should define one of them as the model
// controller.set('model', model.cows); or
// this._super(controller, model.cows); this does the default setupController method
// in this particular case, ArrayController
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句