2016-03-03 30 views
0

所以我正在學習Riot JS,遵循指南。給出一個逐步解釋的例子。 並添加一個「this.update()」來更新暴亂js變量。現在,它爲他工作,但不適合我。你們能告訴我爲什麼嗎?爲什麼我的Riot JS更新不起作用?

這是代碼。

這是index.html的

<body> 
    <script src="bower_components/riot/riot.min.js" type="text/javascript"></script> 
    <script src="tags/all.js" type="text/javascript"></script> 

    <contact-list></contact-list> 

    <script> 

     riot.mount('contact-list', {callback: tagCallback});   

     function tagCallback(theTag) { 
      console.log('callback executed'); 
      var request = new XMLHttpRequest(); 
      request.open('GET', 'people.json', true); 
      request.onload = function() { 
       if(request.status == 200) { 
        var data = JSON.parse(request.responseText); 
        console.log(data); 
        theTag.trigger('data_loaded', data); 
       } 
      } 

      setTimeout(function() { 
       request.send(); 
      },2000); 

     } 
    </script> 
</body> 

這是我接觸list.tag

<contact-list> 
    <h1>Contacts</h1> 
    <ul> 
     <li each={p in opts.people}>{p.first} {p.last}</li> 
    </ul> 


    <script> 
     this.on('mount', function() { 
      console.log('Riot mount event fired'); 
      opts.callback(this); 
     }) 

     this.on('data_loaded', function(peeps) { 
      console.log(peeps); 
      opts.people = peeps; 
      this.update(); 
     }) 

    </script> 
</contact-list> 

與console.logs調試後,我可以看見我正確地檢索數據我的JSON文件,我的聯繫人列表數據在那裏。但子彈列表不會更新。它顯示爲空。

回答

0

哦,沒關係的人,對不起。不知道視頻中這個人的例子究竟是如何運作的。因爲我必須在html觸發事件上傳遞data.people。否則,我正在傳遞一個帶有數組的Array對象。

+0

我認爲你應該跟隨這個帖子的解決方案,並將其標記爲「答案」,以便其他人獲得收益。 另外,您關注哪個指南?我不確定我喜歡使用自定義事件將數據傳遞到標記的樣式,而不是僅通過tag.update()函數將對象作爲參數傳遞。 –

1

是否有任何使用回調函數的原因?

如果不是,將您的回調函數移動到標記中,並在將提取的數據分配給標記變量後直接更新它。

看在riotgear來源: https://github.com/RiotGear/rg/blob/master/tags/rg-include/rg-include.tag

對我來說是一個很好的例子。

+0

感謝您花時間幫助我。這是一個代碼錯誤..請參閱我上面的答案。不知道它如何在教程中的傢伙工作 – msqar