【问题标题】:Ember.js setupController fired only onceEmber.js setupController 只触发一次
【发布时间】:2013-08-14 19:34:02
【问题描述】:

假设我有一个观点:

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 转换到 searchRoute 时),但是如果我已经在searchRoute 这个钩子不会触发,CellarRails.Track 模型的find 方法也不会触发。

【问题讨论】:

    标签: javascript ember.js javascript-framework


    【解决方案1】:

    当您在路由上设置setupController 时,不会调用model 挂钩。如果您希望 modelsetupController 两个钩子都触发,则必须在 setupController 钩子中调用 this._super(...) 以保持 model 钩子默认行为:

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

    希望对你有帮助。

    【讨论】:

    • 看起来我可以使用它,谢谢,但是可以这样做吗?我的意思是它看起来有点老套,不是吗?
    • @Nikita,我知道它可能看起来很老套,但是从 RC4 开始就引入了这种确切的行为:emberjs.com/blog/2013/05/28/ember-1-0-rc4.html,所以你可以这样做:)
    • 问题仍然存在 - 如果我从 applicationRoute 转换到 SearchRoute,它会起作用,但如果我已经在 searchRoute 上并执行 SearchTextFielddidInsertElement,则会触发searchapplicationController 方法,应该触发 transitionToRouteSearchRoute - 它不会发生,setupController 钩子不会触发两次。
    • 我认为我在设计的某个地方犯了一个逻辑错误。
    • @Nikita,嗯,看看你的代码,你好像在使用ember-query lib,这可能与你遇到的行为有关
    【解决方案2】:

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

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

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

    【讨论】:

    • ` 如果路由是通过转换输入的(例如,当使用 linkTo Handlebars 助手时),则已经提供了模型上下文并且不会调用此钩子。`
    • 在我的情况下,路线是通过过渡进入的。无论如何,模型钩子也只触发了一次。
    • 你想从 searchroute 移动到 searchroute 是你的意思吗?在这种情况下,我会考虑使用 {{ action }} 帮助器来控制按钮的作用而不是 linkTo
    • 在这种情况下,我不使用按钮或 linkTo 助手。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 2021-03-22
      • 2015-09-07
      • 2016-11-10
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多