store.find()方法是否使用来自ember-inflector的不同变形规则?

时间:2013-11-18 17:16:30

标签: ember.js ember-data

我正在使用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'时,我总是收到错误消息: 断言失败:未找到“分类”

的模型

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()

我想知道商店的查找方法是否使用了一组不同的变形规则?还是还有其他地方我应该声明此模型的变形规则?

值得一提的是,对此模型的服务器请求是正确的(到正确的URL)并且它们的响应很好。

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

1 个答案:

答案 0 :(得分:7)

问题是当它试图将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