0

假设我有一个观点:

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

4

3 回答 3

3

当您setupController在路线上设置时,model不会调用挂钩。如果你想要这两个钩子model并且setupController应该触发你必须调用this._super(...)你的setupController钩子来保持model钩子的默认行为:

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

希望能帮助到你。

于 2013-08-13T09:13:24.443 回答
0

这是对此问题的解释: https ://github.com/alexspeller/ember-query/issues/9

于 2013-08-14T07:16:46.207 回答
0

尝试使用模型:在 SearchRoute 中挂钩

model: function ( params, transition ) {
    return CellarRails.Track.find(params);
}

如果您直接导航到 url,则会调用此方法,希望所需的参数将在参数中,但请尝试对其进行调试以检查:)

于 2013-08-13T09:04:17.623 回答