模型和相关适配器的 Ember CLI 命名约定



是否可以使用像geo-data这样的模型名称?让我解释一下

我有这样的模型

// app/models/geo-data.js
import DS from 'ember-data';
var geoData = DS.Model.extend({
    name: DS.attr('string')
});
geoData.reopenClass({
    FIXTURES: [
        {
            name: 'foo'
        }
    ]
});
export default geoData;

那我有一条路线

// app/routes/index.js
import Ember from 'ember';
export default Ember.Route.extend({
   model: function() {
       return this.store.find('geo-data');
   }
});

和一个适配器

// app/adapters/geo-data.js
import DS from 'ember-data';
export default DS.FixtureAdapter.extend({});

但我的应用程序一直尝试向http://customhost.com/geoData发送 GET 请求

  1. 首先,它不应该执行任何请求,它在夹具适配器下
  2. 它向/geoData发送请求,而不是/geo-data

肯定错过了一些东西,你能启发我吗?谢谢

默认情况下

,REST 适配器将使您的模型驼化和复数化,以获取关联的终结点。如果要覆盖此功能,可以覆盖适配器。有关所有文档,请参阅 http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_pathForType,但对于你的方案,如果你想保留破折号,你可以做一些类似的事情

export default DS.RESTAdapter.extend({
  pathForType: function(type) {
    var dasherized = Ember.String.dasherize(type);
    return dasherized;
  }
});

至于夹具适配器发送请求的问题,这似乎很奇怪,不应该。我确实注意到您的夹具数据没有id,它绝对应该。下面是使用夹具适配器的示例。

http://emberjs.jsbin.com/firore/1/edit?html,css,js,output

App = Ember.Application.create();
App.Router.map(function() {
  // put your routes here
});
App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('foo');
  }
});

App.FooAdapter = DS.FixtureAdapter.extend({});
App.FooModel = DS.Model.extend({
  name: DS.attr()
});
App.FooModel.reopenClass({
    FIXTURES: [
        {
            id: 1,
            name: 'foo'
        },
        {
            id:2,
            name: 'bar'
        }
    ]
});

还要指出,我过去曾遇到过名为"data"的对象的问题,这些对象可能会导致意外行为。

最新更新