0

我是 EmberJS 的新手,似乎无法弄清楚我的代码有什么问题。我已经阅读并比较了不同的示例项目,希望找到“缺失的链接”,所以我很感激第二眼:

我在用:

  • 车把.js 1.0.0-rc.3
  • ember-latest.js(在 AWS 上)
  • ember-data-latest.js(在 AWS 上)

流血边缘,因为我不断收到网站上的任何错误。

索引.html

<script type="text/x-handlebars" data-template-name="application">
{{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="items">
  <h2>Total entries: {{length}}</h2>
  {{#if length}}
  <ul>
    {{#each item in controller}}
    <li>{{item.title}}</li>
    {{/each}}
  </ul>
  {{/if}}
</script>

应用程序.js

window.App = Ember.Application.create({
  LOG_TRANSITIONS: true
});

// Model
App.Store = DS.Store.extend({
  revision: 12,
  adapter: DS.RESTAdapter.extend({
    bulkCommit: false,
    url:        '/api'
  })
});

App.Item = DS.Model.extend({
  id:    DS.attr('number'),
  title: DS.attr('string')
});

// Router
App.Router.map(function () {
  this.resource('items', function () {
    this.resource('item', { path: ':item_id' });
  });
});

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('items');
  }
});

App.ApplicationRoute = Ember.Route.extend({
  setupController: function() {
    this.controllerFor('item').set('model', App.Item.find());
  }
});

App.ItemsRoute = Ember.Route.extend({
  model: function () {
    return App.Item.find();
  }
});

// Controller
App.ItemsController = Ember.ArrayController.extend({
  sortProperties: ['title']
});

App.ItemController = Ember.ObjectController.extend({
  isEditing: false,

  editItem: function () {
    this.set('isEditing', true);
  }
});

加载时,向 /api/items 发出 XHR 请求,返回如下:

{"items":
  [
    {
      "id":"518c7ceeef56038b77000000",
      "title":"Test 1"
    },
    {
      "id":"518c7ceeef56038b77000001",
      "title":"Test 2"
    }
  ]
}

所以数据正在被检索,但不知何故,它只是没有出现在用户面前!

将不胜感激一些指针。谢谢!

4

1 回答 1

0

我可以想象你的模型是错误的,id如果名称是id. 源代码中的注释如下:

通过使用记录类型调用序列化程序来获取记录的主键名称primaryKey。除非您覆盖该primaryKey方法,否则它将是'id'.

尝试将您的模型更改为此简化版本:

App.Item = DS.Model.extend({
  title: DS.attr('string')
});

希望能帮助到你

于 2013-05-13T18:00:02.433 回答