2013-05-13 63 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') 
}); 

希望它可以幫助