2017-01-23 95 views
0

在我的Meteor.JS应用程序中,我想动态地添加从数据库获取的元素上的点击事件。不幸的是,点击后不会触发函数(但事件被添加到Template.algorithms .__ eventsMap属性中)。我想知道我的方法是否正确,并且可以纠正哪些事件可以触发该事件。Dynamicaly为流星模板添加事件

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 

回答

0

实现是不正确。

解决方案:

虽然循环通过“各自”在模板,附接一类(例如'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'}) 
    } 
}); 

请将脚本转换为玉石和咖啡,这应该工作。