2013-05-13 53 views
0

我是EmberJS的新手,似乎无法弄清楚我的代码出了什么问题。我已阅读并比较了不同的示例项目希望能找到“缺失的一环”,所以我会很感激在这第二组的眼睛:EmberJS不显示来自DS.RestAdapter的结果

我使用:

  • handlebars.js 1.0.0 -rc.3
  • 烬-latest.js(上AWS)
  • 烬数据-latest.js(上AWS)

前沿,因为我不断收到错误,也要与别的网站上。

的index.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> 

app.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); 
    } 
}); 

上的负载,一个XHR请求被制成/ API /项,它返回如下:

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

所以数据正在retr ieved但不知何故,它只是不显示给用户!

希望有些建议。谢谢!

回答

0

我可以想象你的模型是错误的,如果名字是id,那么需要明确指定余烬数据中的id字段。在source code的注释上写着:

通过调用 串行的primaryKey有记录的类型备案获取主键的名称。 除非您覆盖primaryKey方法,否则此 将为'id'

尝试模型改变这种简化版本:

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

希望它可以帮助