【问题标题】:Why doesn't my model hook receive no param?为什么我的模型钩子没有收到任何参数?
【发布时间】:2015-03-05 05:43:10
【问题描述】:

免责声明:主要新手警告!

我已经按照this 教程成功设置了一个 Rails 后端/Ember 前端项目。

我的路由器是

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.resource('contacts', function() {
    this.resource('contact', { path: '/:contact_id' });
  });
});

export default Router;

现在我想设置根据联系人的姓氏仅查找某些联系人的可能性,所以我关注了official docs for specifying query params

我已经修改了我的 app/routes/contacts/index.js 路由,现在它看起来像这样:

import Ember from 'ember';

export default Ember.Route.extend({
  queryParams: {
    lastName: {
      refreshModel: true
    }
  },
  model: function(params) {
    return this.store.findQuery('contact', params);
  }
});

app/controllers/contacts.js

import Ember from 'ember';

export default Ember.ArrayController.extend({

  queryParams: ['lastName'],
  lastName: null,

});

不幸的是,params 没有填充,即使我在浏览器中指定了查询参数。更准确地说,params 对象如下所示:

其中似乎没有任何我感兴趣的内容。

然而,查询参数信息以某种方式传播到视图,所以我感到有些困惑。如何为我的路线中的模型挂钩填充params

【问题讨论】:

    标签: javascript ruby-on-rails ember.js


    【解决方案1】:

    好像this might be a bug。一个简单的解决方法是通过可选的转换参数访问查询参数:

    import Ember from 'ember';
    
    export default Ember.Route.extend({
      queryParams: {
        lastName: {
          refreshModel: true
        }
      },
      model: function(params, transition) {
        return this.store.findQuery('contact', transition.queryParams);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多