2014-02-18 31 views
0

我的模型:使用灰烬数据从节点服务器加载数据异步

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); 
     } 
     }); 

回答

0

如果你可以提供一个jsfiddle,那很好。我不确定是否定义了contactList,以及是否实际定义了该控制器的别名。所以根据我所看到的,我认为问题在于你正在迭代没有正确定义模型的控制器。

我建议尝试做:

{{#each}} 
    <tr> 
     <td>{{contact.name}} </td> 
     <td>{{contact.number}} </td> 
    </tr> 
    {{else}} 
    <tr><td>No contacts yet </td> </tr> 
{{/each}} 

如果你真的想使用contactList控制器,那么你需要确保该App.AllController“需要”的contactListController。

App.ContactListController = Ember.ArrayController.extend({}) // needs to be an array controller 
App.AddController = Ember.ArrayController.extend({ 
    needs: ["contactList"], // controller's used within this controller 
contactList: Ember.computed.alias("controllers.contactList"), // needed to iterate over the model how you're doing it. 

这两种解决方案都应该假设您的数据实际上是在Ember Data中加载的。您可能想要检查ember-data控制台上的“data”选项卡。如果您没有安装该浏览器扩展程序,请执行此操作。这非常有用。

如果一切都失败,尝试登录使用{{登录contactList}}验证预期值

好运

+0

他们没有工作,还我更新的问题作出明确规定烬不是罪魁祸首和服务器没有返回数据是预期的格式。 –