2017-06-12 73 views
0

我有一個初始化函數,我想檢測時,標籤的下拉菜單中刪除(從語義UI):流星在onRendered功能找到this._id

call.js

Template.calledit.onRendered(function() { 
    $('.ui.dropdown').dropdown({ 
     onLabelRemove(value) { 
     console.log(value); 
     console.log(this._id); 
     } 
    }); 
}); 

call.html

<template name="calledit"> 
    <div class="field"> 
    <label>Assign Units</label> 
    <div id="edit_call_assign_units_{{_id}}" class="ui multiple search selection dropdown"> 
     <input type="hidden" name="edit_call_assign_units_{{_id}}" value="{{units}}"> 
     <i class="dropdown icon"></i> 
     <div class="default text">Assign Units</div> 
     <div class="menu"> 
     <div class="item" data-value="420">420</div> 
     <div class="item" data-value="F-117">F-117</div> 
     <div class="item" data-value="805">805</div> 
     <div class="item" data-value="230">230</div> 
     <div class="item" data-value="506">506</div> 
     </div> 
    </div> 
    </div> 
<template> 

然而,我有檢測一個問題它被調用的模板。我如何在onRendered函數中訪問this._id

+0

能否請您補充一點,與此而來的模板代碼? 'this'沒有指向你的Doc,所以this._id可能沒有定義。添加console.log(this)來檢查傳遞給onLabelRemove的對象。 –

+0

@SaeedD。我有控制檯登錄它,但我找不到_id。我也將模板添加到主題中。我有點難倒如何做到這一點:( – DarkTakua

回答

0

貌似錯誤的上下文,也許這

Template.calledit.onRendered(function() { 
    var tmpl = this; 
    $('.ui.dropdown').dropdown({ 
     onLabelRemove(value) { 
     console.log(value); 
     console.log(tmpl._id); 
     } 
    }); 
}); 

或者,如果你使用的ECMAScript,然後

Template.calledit.onRendered(function() { 
    $('.ui.dropdown').dropdown({ 
     onLabelRemove: (value) => { 
     console.log(value); 
     console.log(this._id); 
     } 
    }); 
});