gpt4 book ai didi

javascript - 我将如何根据模型中设置的标志更改路线的 Controller ?

转载 作者:行者123 更新时间:2023-11-30 12:47:24 24 4
gpt4 key购买 nike

我目前设置了一个动态路由:

App.Router.map(function () {
...
this.resource('simpleSearch', {path: 'simplesearch'}, function () {
this.resource('simpleSearchOption', {path: ':simpleSearchOption_id'});
...

我确实设置了 simpleSearchOptionController,其中包含我需要的所有功能和操作,但有一种特殊情况,我需要一些其他初始化和额外功能,否则 Controller 是一样的,所以我扩展了之前的:

App.SimpleSearchOptionController = Ember.ObjectController.extend({

needs: ["simpleSearch"],
simpleSearch: Ember.computed.alias("controllers.simpleSearch"),
// ... Stuff used in the "subclass" as well...
// ...
});

App.SimpleSearchWidthHeightController = App.SimpleSearchOptionController.extend({

init: function () {
this._super();
//... More stuff than the original controller
},
// More functions and so forth that are needed in this case, but not in the "super class'"
//...
});

这是我的路线:

App.SimpleSearchOptionRoute = Ember.Route.extend({    
model: function (params) {
return this.modelFor('simpleSearch').get('simpleSearchOptions')[params.simpleSearchOption_id];
},

setupController: function (controller, model) {
controller.set('model', model);
this.controllerFor('simpleSearch').set('currentQuestion', controller.get('id'));
}
});

I see that I can set the controllerName在 route 。

有没有一种方法可以更改 controllerName路由获取模型对象,但之前它选择并实例化 Controller ,所以我可以根据模型中设置的标志选择 Controller ?

最佳答案

在路由中使用 renderTemplate 钩子(Hook)。

http://emberjs.com/guides/routing/rendering-a-template/

renderTemplate: function(controller, model) {
var controllerName = 'foo';
if(model.get('something') === 'bar'){
controllerName = 'bar';
}
this.render('favoritePost', { // the template to render
controller: controllerName // the controller to use for the template
});
}

现在,你可能需要捕获 Controller 并初始化它,因为你有一个模型,就像这样

renderTemplate: function(controller, model) {

if(model.get('something') === 'bar'){
controller = this.controllerFor('bar');
controller.set('model', model);
// other random stuff you may be doing in setupController
}
this.render('favoritePost', { // the template to render
controller: controller // the controller to use for the template
});
}

关于javascript - 我将如何根据模型中设置的标志更改路线的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com