我的模型:使用灰燼數據從節點服務器加載數據異步
App.Contacts = DS.Model.extend({
name : DS.attr('string'),
number : DS.attr('number')
});
這是我如何保存記錄:
App.AddController = Ember.Controller.extend({
actions : {
addContact : function(){
var post = this.store.createRecord('Contacts',{
name : this.get('name') ,
number : this.get('number')
});
post.save();
}
}
});
加速到Ember的官方指南,這將發出一個POST
請求/Contacts
,所以處理它,我用這個在nodejs/expressjs
app.post('/contacts',function(req,res){
posts.push(req.body);
console.log(posts);
res.send({status: 'OK'});
});
現在我想找回它,進入所謂的all
另一個模板,所以我使用:
App.AllRoute = Ember.Route.extend({
model : function(){
return this.store.find('Contacts');
},
setupController : function(controller,model){
controller.set('contactList',model);
}
});
加至Emberjs指南,示範鉤支持的承諾外的開箱。所以我認爲這應該工作。
我的模板:
<script type="text/x-handlebars" id="all" >
Hello
<table>
{{#each contact in contactList}}
<tr>
<td>{{contact.name}} </td>
<td>{{contact.number}} </td>
</tr>
{{else}}
<tr><td>No contacts yet </td> </tr>
{{/each}}
</table>
</script>
問題
但是這一模式沒有返回,據我所知,this.store.find('Contacts')
不返回JavaScript數組,但本質和目的,implimenting Ember.Enumerable
但在服務器端,posts
是一個JavaScript數組,因此可能會出現類型不匹配。如何解決這個問題?
編輯: 爲了避免客戶端灰燼代碼的混亂,這正常工作,所以有一些問題,往返於服務器。
App.AllRoute = Ember.Route.extend({
model : function(){
return this.store.all('Contacts');
},
setupController : function(controller,model){
controller.set('contactList',model);
}
});
他們沒有工作,還我更新的問題作出明確規定燼不是罪魁禍首和服務器沒有返回數據是預期的格式。 –