Ember Data 使用 store.find 时复数 url 端点



我遇到了一个错误

GET http://localhost:63342/people 404 (Not Found)

Assertion failed: Error while loading route:....

为什么会出现此错误http://localhost:63342/people 404?我没有这条路线。这是我的JS代码:

window.Models = Ember.Application.create();
Models.Router.map(function () {
    //匹配路由后显示person_list模板
    this.resource('persons', { path: '/' });
});
Models.PersonsRoute = Ember.Route.extend({
    model:function(){
        return this.store.find('person');
    },
    setupController:function(controller, model){
        controller.set('content', model)
    }
});

Models.Person = DS.Model.extend({
    name: DS.attr('string'),
    description:DS.attr('string')
});
Models.Person.FEATURES = [{
    name:'Kratos Zhang',
    description:'a c# coder in 7agree.'
}]

和模板

<script type="text/x-handlebars" data-template-name="persons">
<ul>
    {{#each item in persons}}
    <li>
        <h4>{{item.name}}</h4>
        <p>{{item.description}}</p>
    </li>
    {{/each}}
</ul>

为什么它不起作用?

如果适配器

中的 pathForType 不是您的端点,则可以覆盖 pathForType,默认情况下,Ember Data 会复数端点

App.ApplicationAdapter = DS.RESTAdapter.extend({
  pathForType: function(type) {
     //return Ember.String.pluralize(type);
     return type;
  },
});

这不是必需的,您可以将其删除

setupController:function(controller, model){
    controller.set('content', model)
}

模板中不存在人员

{{#each item in model}} 
  <li>
    <h4>{{item.name}}</h4>
    <p>{{item.description}}</p>
  </li>
{{/each}}

{{#each item in controller}}
  <li>
    <h4>{{item.name}}</h4>
    <p>{{item.description}}</p>
  </li>
{{/each}}

最新更新