如何使用on-tap =' node_action&#39 ;?触发元素的点击事件

时间:2015-05-07 16:35:32

标签: javascript-events ractivejs

通常我可以使用fire来激活处理程序,但在这种情况下,我需要在事件处理程序中使用e.context获取完整的项目数据。

更新08.05.15

详细说明
为什么我不能解雇node_action? 我有一个项目列表,然后我想在项目附近的浮动块中显示项目相关信息。 所以每个项目都有on-tap='node_action',我有:

@on 'node_action', (e)->
  item = e.context — then I can use item.id to load data
  item_div = e.node — then I can use item_div.top_y() to show floating menu
                      on the same level on the page with the item div

为什么我需要触发?
如果我想在浮动菜单上工作,我不想在页面刷新后每次手动点击项目,所以我通常会通过触发事件自动完成它。

是的,我可以在这里准备好像

这样的准备活动
e =
  context: ractive.get 'items.0'
  node: ractive.find '.items .line:first'
ractive.fire('node_action', e)

但是可以对列表中的项目进行排序,过滤和更改。

理想情况下,拥有这样的东西会很好:

ractive.find('.items .line:first').tap()

ractive.find('.items .line:first').trigger 'tap' 

或者

ractive.find('.items .line:first').fire 'node_action'

2 个答案:

答案 0 :(得分:0)

在反应式应用程序中,当您想要获取DOM节点以获取数据时,通常会出现问题。但没有更多的细节很难说。

要回答我认为您的问题,您可以使用以下内容从节点获取上下文,keypath和索引,类似于从事件上下文获得的内容:

var info = Ractive.getNodeInfo(node);

答案 1 :(得分:0)

最后发现Rich Harris的Simulant.js实际上是可能的 它不支持tap事件,因此根据我的需要,我添加了一个小的jQuery插件

# Ractive trigger
# btn.r_trigger 'tap'
# btn.r_trigger 'click', {button:1, which:2}
$.fn.r_trigger = (event_name, opt)->
    el = @[0]
    switch event_name
        when 'tap'
            simulant.fire(el, 'mousedown', opt)
            simulant.fire(el, 'click', opt)
        else
            simulant.fire(el, event_name, opt)
相关问题