没有下划线的Ember ActiveModelAdapter URL?

时间:2014-07-22 08:38:23

标签: javascript ember.js

我有这个json响应GET-request => /清单

{"check_lists": [
    {
        "id": 1,
        "name": "Example-list-1",
        "description": ""
    },
    {
        "id": 2,
        "name": "Example-list-1",
        "description": ""
    }
}

要处理此服务器的强调命名约定,我使用ActiveModelAdapter和ActiveModelSerializer。

我遇到的问题是请求网址。 我的模型名为App.CheckList = DS.Model.extend({......这就是它开始变得复杂的地方。 如果我打电话

return this.store.find('checkList');

在我的路线中,Ember向/ checkLists路线发起GET请求,而不是/ check_lists =>

GET http://localhost:3000/checkLists 404 (Not Found)
Error while processing route: checklists 

令人惊讶的是这个错误

buildURL: function(type, id) {
    return this._super(type, id);
},

未使用,因此我没有机会修改网址。

有谁知道如何将请求更改为/ check_lists ??

1 个答案:

答案 0 :(得分:1)

驼峰案例和强调案例的映射在ActiveModelAdapter.pathForType函数中完成。您可以覆盖它并在那里进行更改。例如,从驼峰案例改为强调案例:

App.ApplicationAdapter = DS.ActiveModelAdapter.extend({
  pathForType: function(type) {
    var decamelized = Ember.String.decamelize(type);
    var underscored = Ember.String.underscore(decamelized);

    //Alternatively, you can change urls to dasherized case using this line
    //var dasherized = Ember.String.dasherize(decamelized);

    return Ember.String.pluralize(underscored);
  }
});

奇怪的是,最新的Ember数据已在ActiveModelAdapter中包含此代码。您可能需要检查正在运行的版本并升级到该版本,而不是我上面建议的更改。