2016-12-02 52 views
0

我看過很多類似的問題,但仍然難以完成。 Try-d observable-s,但搞亂了某處並且無法完成。新仍然Riotjs從子標記到父標記的數據

riotjs在子標籤,我有,將資料列表的功能:

<make-list> 
...lots of html... 
<script> 
    var piclist = []; --after first function run this list has data 
    .... 
    done: function (e, data) { 
         piclist.push(data.result); 
        } 
    ... 
</script> 
</make-list> 

,並在父母的數據,我想訪問它在功能

<main> 
...lots of html.. 
<script> 
riot.mount('make-list') 

and i wana use that piclist = []; list here inside a function 

</script> 

</main> 

回答

1

通過使用mixin完成了這種操作。也許這不是正確的方式,但它的工作原理。

<main> 
...lots of html.. 
<script> 
riot.mount('make-list') 

var piclist = []; 
riot.mixin(piclist) 


</script> 

</main> 
<make-list> 
    ... lots of html ... 
    <script> 
    ... 
     done: function (e, data) { 
     piclist.push(data.result); 
     } 
    ... 
    </script> 
</make-list> 
0

似乎您希望make-list標記爲mainmake-list顯示的列表創建列表項目,並且將成爲main的子項。

您在父標籤內使用riot.mount('make-list')。這至少非常不尋常:它實際上會觸發要掛載的頁面上的所有make-list標籤。爲什麼不採用暴亂的方式並將其添加到父級的HTML部分中,像這樣?

<main> 
    ... lots of html ... 
    <make-list opts={piclist} /> 

    <script> 
    this.piclist = []; 
    </script> 
</main> 

opts允許您將數據傳遞給子(在本例中是對列表的引用)。你可以像這樣訪問子標籤內的內容:

<make-list> 
    ... lots of html ... 
    <script> 
    ... 
     done: function (e, data) { 
     this.opts.piclist.push(data.result); 
     } 
    ... 
    </script> 
</make-list> 

我希望這有助於。

+0

謝謝你的幫助,但這不是我要找的。 make-list.tag列出項目,我需要在我的main.tag函數中... 我需要將數據從子標記傳遞給父標記。 你顯示的方式,數據不會從子標記傳遞給父標記...說:未捕獲的TypeError:無法讀取未定義的屬性'piclist'(...) – Rix

+0

從技術上講,您不會將數據從子項傳遞給父項,你可以將一個父對象的引用傳遞給子對象,以便兩者都可以訪問這些數據。也許你得到的錯誤是因爲'make-list'腳本部分的其他部分。也許這不是指實際的標籤。嘗試在腳本部分的開頭設置var'tag = this',然後使用'tag.opts'而不是'this.opts' – moritz