我的模型:使用灰烬数据从节点服务器加载数据异步
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);
}
});
他们没有工作,还我更新的问题作出明确规定烬不是罪魁祸首和服务器没有返回数据是预期的格式。 –