【问题标题】:Does store.find() method use different inflection rules from ember-inflector?store.find() 方法是否使用与 ember-inflector 不同的变形规则?
【发布时间】:2013-11-18 17:16:30
【问题描述】:

我正在使用 Ember v1.0.0 和 Ember-Data v1.0.0-beta.3 构建一个简单的 Ember 应用程序。我有一个名为“Categoria”的模型

categoria.coffee:

App.Categoria = DS.Model.extend
  nombre: DS.attr 'string'
  simplified: DS.attr 'boolean'
  candidatos: DS.hasMany 'candidato'

当我尝试通过它的 id 查找“类别”时,我总是收到错误消息: 断言失败:找不到“类别”的模型

categoria_route.coffee:

App.CategoriaRoute = Em.Route.extend(
  model: (params)->
    @store.find('categoria', params.categoria_id)
)

App.CategoriaIndexRoute = Em.Route.extend(
  model: ->
    categoria = @modelFor('categoria')
    categoria.reload() if categoria.get('simplified') 
    categoria
)

由于是西班牙语命名的模型,我已经指定了变形规则。

store.coffee:

Ember.Inflector.inflector.irregular('categoria', 'categorias')
Ember.Inflector.inflector.rules.irregularInverse['categorias'] = 'categoria'
App.ApplicationAdapter = DS.ActiveModelAdapter()
App.ApplicationSerializer = DS.ActiveModelSerializer.extend()

我想知道 Store 的 find 方法是否使用了一组不同的变形规则? 或者还有其他地方我应该为这个模型声明变形规则吗?

值得一提的是,针对此模型向服务器发出的请求是正确的(发送到正确的 URL),并且它们的响应格式正确。

我尝试对store.find 方法使用不同的语法,如Ember Guides#Method Find (http://emberjs.com/api/data/classes/DS.Store.html#method_find) 中所述,但错误是相同的。

【问题讨论】:

  • 这很有趣,它使用了 ember 变形器规则。让我看看我们能不能帮你找到它。
  • @kingpin2k:谢谢!我为这个问题浪费了一天的时间,我从没想过“Ember.Inflector.inflector.singular”,但效果很好!

标签: ember.js ember-data


【解决方案1】:

问题是当它尝试单数化 json 响应时,它尝试单数化 categoria 并返回 categorium,您的复数化工作正常。

var iff = Ember.Inflector.inflector;

iff.irregular('categoria', 'categorias');
// this adds a test for when it finds categoria and you are trying to singularize, return categoria
iff.singular(/categoria/, 'categoria');

http://emberjs.jsbin.com/ICeGuzuX/3/edit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2011-04-14
    • 2018-01-08
    • 2017-05-13
    • 2017-08-03
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多