2014-04-14 42 views
0

我想創建一個Ember.js應用程序第一次使用RESTful後端。我一直在使用從我的服務器呈現的HTML,因此我是一個新手燼。 我有一個基於所有者ID獲取餐廳的查詢,然後在標題欄中顯示名稱。Ember數據從休息URL不呈現

LiveKitchen.RestaurantController = Ember.ObjectController.extend({ 
    needs : 'login' 
}); 

LiveKitchen.RestaurantRoute = Ember.Route.extend({ 
model: function() { 
    console.log(this.controllerFor('login').get('user_id')); 
    console.log('Fetching Restaurants ' + this.store.find('restaurant', {owner_id : this.controllerFor('login').get('user_id')})); 
    var res = this.store.find('restaurant', {owner_id : this.controllerFor('login').get('user_id')}); 
    // restaurant.tables = this.store.find('table', {restaurant_id : restaurant.mongo_id}); 
    console.log('Restaurant ' + res); 
    return res.objectAt(0); 
    // return restaurant; 
} 
}); 

我已驗證了使用調試器的響應,並且看到json數據正確返回。然而,我的模板

<script type="text/x-handlebars" data-template-name="restaurant"> 
    <small> 
     <i class="icon-leaf"></i> 
     {{name}} 
     </small> 
</script> 

沒有在頁面上打印的名稱。

你可以請我幫助我如何調試,並找出爲什麼數據不呈現?由於沒有錯誤,我無法找出接下來要採取的步驟。

+0

'this.store.find('restaurant')'返回餐廳模型的多個實例嗎? – NicholasJohn16

+0

最初,我以{「restaurant」:{「name」:「SomeName」,...}}的形式返回了一家餐廳,該餐廳無法使用。所以我認爲它可能會期待很多,並開始返回{「餐館」:[{「name」:「SomeName:,...}]} – Manoj

+0

我嘗試了其他的東西,而不是使用owner_id,我用對象的ID並使用this.store.find('restaurant',id),然後名稱來了。雖然從兩種情況返回的數據是相同的從REST API,燼數據似乎期望不同的結果? – Manoj

回答

0

,問題可能會使用objectAt(0),嘗試做這樣的代替:

return res.get('firstObject'); 
+0

我試過這個,我得到的值爲null,返回的響應雖然看起來正確,但似乎沒有被ember理解,這可能是問題所在。我曾經使用ArrayController嘗試過一個非常基本的情況,然後返回正在工作的列表。 – Manoj

0

終於想通了。感謝您的指針,我不得不做res.get('firstObject'),但不是在模型回調中,而是在setupController回調中。看起來如果我使用像我這樣的查詢,它將得到一個無法分配給對象控制器的promise數組。另外,因爲它是一個承諾,所以直到承諾完成,setupController將不會被調用。因此,使用以下方式的setupController回調:

LiveKitchen.RestaurantRoute = Ember.Route.extend({ 
model: function() { 
    console.log(this.controllerFor('login').get('restaurant_id')); 
    console.log('Fetching Restaurants '); 
    //var res = this.store.find('restaurant', this.controllerFor('login').get('restaurant_id')); 
    var res = this.store.find('restaurant', {"owner_id" : this.controllerFor('login').get('user_id')}); 
    console.log('Restaurant ' + res); 
    return res; 
}, 
setupController : function(controller, model) { 
    console.log('Incoming ' + model.get('firstObject').get('name')); 
    controller.set('model', model.get('firstObject')); 
} 

});

現在它工作。發佈它可以幫助其他人前進。