【问题标题】:Dynamicaly add event to Meteor template动态添加事件到 Meteor 模板
【发布时间】:2017-06-08 10:36:24
【问题描述】:

在我的 Meteor.JS 应用程序中,我想在从数据库中获取的元素上动态添加点击事件。不幸的是,单击后未触发功能(但将事件添加到 Template.algorithms.__eventsMap 属性)。我想知道我的方法是否正确以及可以纠正什么以触发该事件。

main.coffee:

Template.algorithms.onCreated ->
  Template.instance().subscribe('algorithm-descriptions', {
    onReady: () ->
      for alg in AlgorithmDescriptions.find().fetch()
        Template.algorithms.events({
          "click .#{alg.button}": () ->
            $(".#{alg.divClass}").scrollintoview({duration: 'slow'})
    })
})

algorithms.jade:

.col-md-2
  ul
    each alg in algorithmDescriptions
      li(class=alg.button)=alg.name
.col-md-10
  each alg in algorithmDescriptions
    div(class=alg.div)
      h2=alg.name

【问题讨论】:

    标签: javascript meteor coffeescript pug


    【解决方案1】:

    实现不正确。

    解决方案:

    在遍历模板中的“每个”时,附加一个类(例如'clickHere')并将文档的id 提供给它。

    例子

    template.html - (对不起!我没用过玉)

       {{#each alg}}
                <div class="clickHere" id="{{id}}">
                     h2={{name}}
                 </div>
            {{/each}}
    

    template.js(对不起!我也没用过coffeescript)

    Template.algorithms.events({
         "click .clickHere" : function(){
             var id=this._id;
            $("#"+id).scrollintoview({duration: 'slow'})
         }
    });
    

    请将脚本转换为翡翠和咖啡,这应该可以。

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多