【问题标题】:Binding Semantic-UI module events in Meteor Template.events在 Meteor Template.events 中绑定 Semantic-UI 模块事件
【发布时间】:2015-12-29 08:40:38
【问题描述】:

我不想使用 jQuery,而是想将回调绑定到 Semantic-UI 模块的“Meteor-way”,有点像 Bootstrap 3 允许的,例如:

Template.someTemplate.events({
  'show.bs.dropdown .someDropdown': function () {
    // official Bootstrap JS callback for the Dropdown module's show event
  }
});

使用 Semantic-UI 似乎我只能在包含模板的 onRendered() 回调中使用 jQuery 绑定时定义回调函数,例如:

Template.someTemplate.onRendered( function () {
  this.$('.someDropdown').dropdown({
    'onShow': function () {
      // official Semantic-UI JS callback for the Dropdown module's show event
    }
  });
});

这不是最优的,因为 (a) 在特定模板的 onRendered() 回调期间其他 DOM 元素可能还没有准备好进行操作,并且 (b) 它使得关注点分离和代码清晰变得更加难以实现。

有没有办法使用 Meteor 的 Template.events 绑定这些事件,如果是,那么正确的语法是什么?

谢谢。

【问题讨论】:

    标签: javascript jquery meteor meteor-blaze semantic-ui


    【解决方案1】:

    我的解决方案

    html

    <div class="ui search selection dropdown" id="dataDropdown">
      <input type="hidden" name="applications">
      <i class="dropdown icon"></i>
      <div class="default text">Application</div>
      <div class="menu" >
        {{#each data}}
          <div class="item" data-value={{id}}>
            {{name}}
          </div>
        {{/each}}
      </div>
    </div>
    

    js

    //can catch 2 events when searching then clicking on a dropdown item. 
    //first event is the change of the input text. second event is the dropdown
    //change. we only want the values associated with the dropdown change, thus
    //we use the if check
    change #appDropdown': function(event, template) {
      if (event.target.type === 'text'){
        return;
      }
      console.log(event.target.value);
    }
    

    【讨论】:

      【解决方案2】:

      请参考this Solution

      如解决方案中所述,绑定 UI 的最佳方法是使用 manuel:viewmodel

      您将 UI 的状态保存在 javascript 对象中,并将 UI 元素绑定到该对象的属性。

      文档可以在here找到

      【讨论】:

      • 感谢您的回复。这是一个好看的包。但是,将自定义变量和函数绑定到模板 DOM 并不是我想要的。我只是在寻找 Meteor 事件驱动的语法来绑定本机 Semantic-UI 模块事件。我想避免添加引入更多复杂性的额外包。我需要的 DOM 的任何反应变量和助手都已经是模板的一部分,这使得 viewmodel 包不完全适合我刚才的目的。
      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2023-03-06
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多