2014-02-18 32 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

他們沒有工作,還我更新的問題作出明確規定燼不是罪魁禍首和服務器沒有返回數據是預期的格式。 –