Ember - 无法使用RESTAdapter加载数据

时间:2014-06-14 09:56:55

标签: ember.js

我现在一直在寻找我的问题的答案一天,我无法解决它。我在这里看过一些相关的主题,但没有一个能解决我的问题。

我无法让DS.RESTAdapter在我的ember应用中运行。我在控制台中获得以下日志

Attempting URL transition to / ember.js:3285
Transition #1: Beginning validation for transition to users ember.js:3285
Transition #1: application: calling beforeModel hook ember.js:3285
Transition #1: application: resolving model ember.js:3285
Transition #1: application: calling afterModel hook ember.js:3285
Transition #1: application: validation succeeded, proceeding ember.js:3285
Transition #1: users: calling beforeModel hook ember.js:3285
Transition #1: users: resolving model ember.js:3285
Ember Debugger Active VM4904:161
XHR finished loading: GET "http://localhost/ember2/api/users". jquery-1.10.2.js:8706
Transition #1: users: transition was aborted ember.js:3285
Transition #1: users: handling error: [object Object] ember.js:3285
Error while loading route: undefined 




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


App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: "ember2/api"
});



//User model
App.User = DS.Model.extend({
    title: DS.attr('string'),
    isCompleted: DS.attr('boolean')
});


//Router
App.Router.map(function() {
    this.resource('users', {path: "/"});
});

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




//The json response
{
  "users": (I have tried users and user)
  {
   "id": 1
   "title": "Learn Ember.js",
   "isCompleted": true
  }, 
  {
   "id": 2
   "title": "Learn Ember.js",
   "isCompleted": true
  }
}

如果有人能够朝着正确的方向推动我,我将不胜感激!

旁注:如果我使用灯具一切正常!

发现我做错了什么。

问题是我格式化的json响应不佳。正确的语法是:

{
  "user": [
     {
    "id": 1,
    "title": "Learn Ember.js",
    "is_completed": true
     },
     {
    "id": 2,
    "title": "Learn Ember.js",
    "is_completed": true
     }
   ]
}

1 个答案:

答案 0 :(得分:0)

只是要指出那个遇到问题的人,那就是他的属性是一个对象,当它应该是一个数组时。从技术上讲,它也应该是复数。

不正确

{
  "users": {  /// <---- users should be an array

正确

{
  "users": [
     {
      "id": 1,
      "title": "Learn Ember.js",
      "is_completed": true
     },
     {
      "id": 2,
      "title": "Learn Ember.js",
      "is_completed": true
     }
   ]
}

可在此处找到更多示例:https://github.com/emberjs/data/blob/master/TRANSITION.md