升级到ember-data beta2时出错

时间:2013-09-17 17:02:02

标签: ember.js ember-data

尝试通过restful api加载一组记录时出现此错误。它适用于ember-model但是当我切换到ember-data(beta 2)和ember.js(1.0)时,我得到了这个错误。

断言失败:未找到“0”的模型

App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: 'http://localhost:8080',
  namespace: 'api'
});


App.Router.map(function() {
  this.resource('about');
  this.resource('pages', function() {
      this.resource('page', { path: ':page_id' });
  });
this.resource('login');
});

App.AuthenticatedRoute = Ember.Route.extend({
  actions: {
     error: function(reason, transition) {
      if (reason.status == 403) {
          alert('You must login');
          this.transitionTo('login');
      }
      else {
          //alert('non 403 error:'+reason.status);
          this.transitionTo('login');
      }
     }
  }
});


App.PagesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('page');
  }
});

App.PageRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('page', params.page_id);
  }
});

App.Page = DS.Model.extend({
  user: DS.attr('string'),
  slug: DS.attr('string'),
  zcontent: DS.attr(),
  title: DS.attr('string'),
  create_time: DS.attr('date'),
  update_time: DS.attr('date')
});

这是后端返回的内容:

{
    "pages": [
        {
            "update_time": "2013-01-14 19:59:35.744855",
            "title": "",
            "id": 38,
            "create_time": "2012-08-04 15:08:16",
            "user": "robert-jones",
            "slug": "orange"
        },
        {
            "update_time": "2013-01-14 19:59:35.899830",
            "title": "",
            "id": 47,
            "create_time": "2012-08-03 10:04:07",
            "user": "robert-jones",
            "slug": "REVIEWStheraputics"
        },
        {
            "update_time": "2012-12-18 07:16:13.656509",
            "title": "'Notes on Temperature Heat Map'",
            "id": 17,
            "create_time": "2012-12-18 07:14:48.118119",
            "user": "robert-jones",
            "slug": "50612c27b94ca650"
        },
        {
            "update_time": "2012-12-12 19:48:08.079021",
            "title": "'Notes on Dyanmic Heat Map'",
            "id": 8,
            "create_time": "2012-12-12 19:48:08.013336",
            "user": "robert-jones",
            "slug": "c5642b4c14d901cf"
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

在我的服务器刚刚返回原始数组而不是带有根元素的对象之前,我遇到了这个问题。也就是说,而不是:

{pages : [...]}

我不小心回来了:

[...]

如果没有JSBin的话,很难说。