假设我有一个观点:
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
钩子仅在第一次触发(当我从applicationRoute
to转换时searchRoute
),但是如果我已经在searchRoute
这个钩子中不会触发并且模型find
的方法CellarRails.Track
也不会触发。