[英]Ember.js find() does not work with parameters
我正在嘗試使用ember的簡單帖子檢索應用程序。 只要我不將參數傳遞給find()函數,一切都可以正常工作:
這很好用:
return this.store.find('post')
但不是這些:
return this.store.find('post', { title: 'title1' })
return this.store.find('post', {})
無論參數如何,服務器都會返回完全相同的JSON,但是當有參數時,Ember似乎不會對其進行處理。 商店空着。
這是完整的代碼:
App = Ember.Application.create({
LOG_TRANSITIONS: true
});
App.Router.map(function(){
this.resource('myPosts');
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api/v1/'
});
App.Post = DS.Model.extend({
title : DS.attr( 'string' ),
body : DS.attr( 'string' )
});
App.MyPostsRoute = Ember.Route.extend({
model : function() {
return this.store.find('post', { title: 'title1' })
},
setupController: function (controller, model) {
controller.set('model', model);
}
});
在所有情況下,我都不會收到任何錯誤,查詢處理也很好,並且我在chrome DevTools中檢查了服務器返回的JSON是否相同。
這是返回的JSON。
{"post": [{"body": "a body", "title": "title1"}]}
車把模板很簡單:
<script type="text/x-handlebars" id="myPosts">
<div class = "postsWrapper">
{{#each}}
<div>{{this}}</div>
<div>title: {{title}}</div>
{{/each}}
</div>
</script>
這是我通過this.store.find('post',{title:'title1'})獲得的輸出:
<App.Post:ember382:null>
title:
這是我沒有參數的情況:
<App.Post:ember294:null>
title: title1
感謝您的幫助。
在進一步交談之后,我認為您的問題已鏈接到API。
調用this.store.find('post')
Ember應該向api/v1/posts
端點發出GET
請求,該端點應返回以下JSON:
注意:這是帶有post對象數組的復數posts對象。
{
"posts": [
{ "body": "body1", "title": "title1" },
{ "body": "body2", "title": "title2" }
]
}
調用this.store.find('post', { title: 'title1' })
Ember應該向api/v1/posts?title=title1
端點發出GET
請求,該端點應返回以下JSON:
注:這是一個奇異物體后用單篇文章的對象。
{
"post": { "body": "body1", "title": "title1" }
}
JSON從服務器返回的格式非常重要,API需要進行過濾,除非您要求,否則Ember不會進行過濾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.