【发布时间】:2014-02-18 12:48:43
【问题描述】:
我的模特:
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 的官方指南,这会向 /Contacts 发送一个 POST 请求,所以为了处理它,我在 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 指南,模型钩子支持开箱即用的 Promise。所以我认为这应该可行。
我的模板:
<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数组,而是本质上和对象,暗示Ember.Enumerable
但在服务器端,posts 是一个 javascript 数组,因此在这之间可能存在类型不匹配。如何解决?
编辑: 为了避免客户端 Ember 代码中的任何混淆,这可以正常工作,因此往返服务器存在一些问题。
App.AllRoute = Ember.Route.extend({
model : function(){
return this.store.all('Contacts');
},
setupController : function(controller,model){
controller.set('contactList',model);
}
});
【问题讨论】:
标签: node.js ember.js express ember-data