2016-09-14 44 views
0

我在火焰爲button s的一個動態模板,如下所示(簡化):傳遞動態數據屬性來內容塊在流星

button.html

<template name="Button"> 
    <button {{attributes}}> 
    <span class="button__text">{{> UI.contentBlock}}</span> 
    </button> 
</template> 

button.js

import {Template} from 'meteor/templating'; 
import cx from 'classnames'; 

import './button.html'; 

Template.Button.helpers({ 
    attributes() { 
    const instance = Template.instance(); 
    let {data} = instance; 

    return { 
     disabled: data.disabled, 
     'class': cx('button', data.class) 
    }; 
    } 
}); 

嘗試設置動態數據屬性:

{{#Button class="js-add-contact" data-phase-index={{index}}}}Add Contact{{/Button}} 

index這種插入(假設它只是一個簡單的,動態的字符串)到data-phase-index拋出一個錯誤:內容分塊沒想到的{{。我不確定另一種將動態數據導入模板的方法。還有在attributes()助手中獲取Button識別的data-屬性的問題。任何人都可以清除它?

+0

你能不能把'index'的值轉換成一個幫手,並在ATTR'數據相指數= getIndex'參考呢?這在'Template.dynamic'中適用於我。 – CodeMonkey

+0

@CodeMonkey感謝輸入,索引實際上是一個幫手,我只是犯了一個愚蠢的錯誤! – chazsolo

回答

1

只需data-phase-index=index應該工作。

既然你已經是雙花括號內爲您Button調用模板,流星知道它會得到解釋值。例如,看到你必須在class="js-add-contact"的字符串周圍使用引號。

像往常一樣,將流星嘗試從助手解釋index,或從數據上下文。

+0

簡直不敢相信!絕對解決了拋出的錯誤。然而,我的問題的第二部分仍然困擾着我 - 當試圖訪問數據屬性時,即使使用您的方法,他們也會回到'未定義'。如果我在單擊按鈕時使用console.log event.target.dataset,它會打印DOMStringMap {}' – chazsolo

+0

啊,不要緊,它是'button__text'引發的事件,它沒有任何屬性。感謝幫助! – chazsolo

+1

yw :-)感謝您的反饋。我很高興你解決了你的問題的第二部分。 – ghybs