[英]Materialize material_select() not working before refresh in Meteor app
我在Meteor項目的模板中使用了以下select
<div class="input-field">
<select name="color" id="color">
<option value="%23B1365F">Pink</option>
<option value="%232952A3">Blue</option>
<option value="%23711616">Red</option>
<option value="%2328754E">Green</option>
<option value="%23BE6D00">Orange</option>
<option value="%23113F47">Sea Blue</option>
<option value="%235229A3">Purple</option>
<option value="%23528800">Olive</option>
<option value="%2388880E">Gold</option>
<option value="%23333333">Black</option>
</select>
<label for="color">Colour: </label>
</div>
在我的js文件中使用以下命令初始化Materialize選擇下拉列表
if (Meteor.isClient) {
Template.layout_settings.onRendered(function () {
$('select').material_select();
});
}
但是,選擇對話框僅在刷新后有效/顯示,刷新之前,我在控制台中收到以下錯誤:
TypeError: $(...).material_select is not a function
at null.<anonymous> (settings.js:48)
at template.js:116
at Function.Template._withTemplateInstanceFunc (template.js:457)
at fireCallbacks (template.js:112)
at null.<anonymous> (template.js:205)
at view.js:107
at Object.Blaze._withCurrentView (view.js:538)
at view.js:106
at Object.Tracker._runFlush (tracker.js:497)
at onGlobalMessage (setimmediate.js:102)
刷新后,它可以很好地工作,是否有人知道為什么會發生這種情況? 在Google上找不到任何內容。
提前致謝!
這不是最漂亮的解決方法,但是它解決了該問題。
Materialze似乎沒有及時添加其jQuery函數以在Blaze模板的onRendered函數中使用它們。
但是,它確實將函數添加到Package.jquery.$.fn.material_select
對象。
我剛剛創建了自己的jQuery函數,該函數引用了該對象的函數體:
(function($){
$.fn.material_select_fix = Package.jquery.$.fn.material_select;
})(jQuery)
$("select").material_select_fix();
我希望這也適用於其他情況...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.