gpt4 book ai didi

javascript - 在 Ember 的操作中处理事件绑定(bind)和回调

转载 作者:行者123 更新时间:2023-11-28 01:46:11 25 4
gpt4 key购买 nike

这闻起来。此操作的结果应该在页面上注册一个事件处理程序,但是我应该放置命名函数回调(clickHandler)吗?混合?或者也许完全是另一个类。

App.StepController = Ember.ObjectController.extend({
actions: {
captureStep: function() {

//Bind the handler to this so callback has access to Controller properties
var clickHandler = clickHandler.bind(this);

//Can't do this with jquery, as capture is not cross browser compatible.
//Setting capture to true on a body event makes this event fire before all others
if (document.body.addEventListener) {
document.body.addEventListener('click', clickHandler, true);
}

function clickHandler(event) {
event.preventDefault(); //Needed for links/buttons
event.stopImmediatePropagation(); //Stop the event from bubbling and prevent 'same element' event handlers

//Determine clickTarget...

console.log("Clicked: " + clickTarget);
this.set('model.targetPath', clickTarget);

// remove this handler from the body
document.body.removeEventListener(event.type, clickHandler, true);
}
}
}
});

最佳答案

Ember views will handle the page events为你;无需编写自己的事件处理程序:

App.StepView = Ember.View.extend({
click: function(event) {
this.get('controller').set(...);
}
}
});

或者,您可以使用links或行动actions处理点击事件。

关于javascript - 在 Ember 的操作中处理事件绑定(bind)和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com