gpt4 book ai didi

javascript - Ember.js setupController 仅触发一次

转载 作者:行者123 更新时间:2023-11-28 20:21:34 25 4
gpt4 key购买 nike

假设我有一个观点:

CellarRails.SearchTextField = Ember.TextField.extend({
templatename: 'index',
insertNewline: function(){
this.get('controller').set('query', this.get('value'));

// calling search method of application controller
this.get('controller').send('search');
this.set('value', '');
}
});

还有一个applicationController:

CellarRails.ApplicationController = Ember.Controller.extend({
needs: ['search'],
query: '',

// here is the search method
search: function() {

// I'm using ember-query lib, which provides this helper, it acts just like usual transitionToRoute
this.transitionToRouteWithParams('search', {
q: this.get('computedQuery')
});
},
computedQuery: function() {
this.get('controllers.search').set('q', this.get('query'));
return this.get('query');
}.property('query')
});

所以现在应该转换为searchRoute:

CellarRails.SearchRoute = Ember.Route.extend({
serializeParams: function(controller) {
return {
q: controller.get('q')
};
},
deserializeParams: function(params, controller) {
controller.set('q', params.q);
},

// pass CellarRails.Track model to SearchController's context
setupController: function(controller, context, params) {
console.log('setup controller hooked!');
controller.set('context', CellarRails.Track.find(params));
}
});

CellarRails.Track模型中,我重新定义了find方法。问题:此代码有效,但 setupController Hook 仅在第一次触发(当我从 applicationRoute 转换到 searchRoute 时>),但是如果我已经在 searchRoute 中,这个钩子(Hook)不会触发并且 CellarRails.Trackfind 方法模型也不会启动。

最佳答案

当您在路线上设置 setupController 时,不会调用 model Hook 。如果您希望同时触发 modelsetupController 钩子(Hook),则必须在 setupController 中调用 this._super(...) Hook 来维护 model Hook 默认行为:

CellarRails.SearchRoute = Ember.Route.extend({
...
model: function(params) {
return CellarRails.MyModel.find();
},
setupController: function(controller, model) {
this._super(controller, model);
...
}
...
});

希望有帮助。

关于javascript - Ember.js setupController 仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204791/

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