Javascript Famo.us附加点击和触摸启动事件
我想知道如何将事件“单击”和“触摸启动”绑定到一行中:Javascript Famo.us附加点击和触摸启动事件,javascript,famo.us,Javascript,Famo.us,我想知道如何将事件“单击”和“触摸启动”绑定到一行中: Engine.on("click"), function(){ aCode() } Engine.on("touchstart", function(){ aCode() } 我期望类似的东西(因为它在其他一些框架中实现): 我当前的解决方法是链接它们: Engine.on("click", function(){Engine.emit("touchstart")}); Engine.on("touchstart", function()
Engine.on("click"), function(){ aCode() }
Engine.on("touchstart", function(){ aCode() }
我期望类似的东西(因为它在其他一些框架中实现):
我当前的解决方法是链接它们:
Engine.on("click", function(){Engine.emit("touchstart")});
Engine.on("touchstart", function() { aCode() }
有更好的做法吗?问题是,无论是在iOS上还是在桌面上使用鼠标的touchstart上,都无法捕捉到单击……显然,无论是什么设备,我都希望以相同的方式处理事件。Famo.us中的on()
方法只接受一种事件类型。它不做任何jQuery风格的字符串处理来确定单独的事件。理论上,您可以有一个名为“到达斩波器”的事件
但是,在创建自定义视图时,我要做的是创建一个bindEvents()
函数,将所有事件侦听器分组在一起。这些处理程序中唯一的代码是其他函数。如果我想以相同的方式对两个不同的事件做出反应,我会对它们使用相同的函数
// An example Class demonstrating events - some off-topic parts omitted
function myLovelyHorseButton() {
// Recomended Reading: https://famo.us/guides/dev/events.html
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
this._eventInput.pipe(this.sync);
this.sync.pipe(this._eventInput);
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
/** Set options & variables
/* ...
*/
// create event listeners for this specific instance
_bindEvents.call(this);
};
// bind all the events for the button
function _bindEvents() {
//Call event handlers with this set to owner.
this._eventInput.bindThis(this);
// listen for events
this._eventInput.on('click', _handleClick);
this._eventInput.on('touchstart', _handleClick); //Same as 'click'
};
// Nay to the console on click/touch
function _handleClick(event) {
console.log('Nayyyyy!!!');
};
// create an instance of myLovelyHorseButton
var button = new myLovelyHorseButton;
// We would then add the button to the Render Tree
mainContext.add(button);
您不想使用当前使用的链接模式有一个很大的原因。这是因为通过在点击时发出一个touchstart事件,您可以推断出将有一些代码对其进行操作。有一天你可能会喝醉,决定“触摸设备上没有人在使用它!”并删除ontouchstart
处理程序。在一瞬间,你的代码对任何人都不起作用,无论是触摸还是鼠标
TL;DR使用多个on()
调用没有错
我希望这能有所帮助。Famo.us中的on()
方法只接受一种事件类型。它不做任何jQuery风格的字符串处理来确定单独的事件。理论上,您可以有一个名为“到达斩波器”的事件
但是,在创建自定义视图时,我要做的是创建一个bindEvents()
函数,将所有事件侦听器分组在一起。这些处理程序中唯一的代码是其他函数。如果我想以相同的方式对两个不同的事件做出反应,我会对它们使用相同的函数
// An example Class demonstrating events - some off-topic parts omitted
function myLovelyHorseButton() {
// Recomended Reading: https://famo.us/guides/dev/events.html
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
this._eventInput.pipe(this.sync);
this.sync.pipe(this._eventInput);
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
/** Set options & variables
/* ...
*/
// create event listeners for this specific instance
_bindEvents.call(this);
};
// bind all the events for the button
function _bindEvents() {
//Call event handlers with this set to owner.
this._eventInput.bindThis(this);
// listen for events
this._eventInput.on('click', _handleClick);
this._eventInput.on('touchstart', _handleClick); //Same as 'click'
};
// Nay to the console on click/touch
function _handleClick(event) {
console.log('Nayyyyy!!!');
};
// create an instance of myLovelyHorseButton
var button = new myLovelyHorseButton;
// We would then add the button to the Render Tree
mainContext.add(button);
您不想使用当前使用的链接模式有一个很大的原因。这是因为通过在点击时发出一个touchstart事件,您可以推断出将有一些代码对其进行操作。有一天你可能会喝醉,决定“触摸设备上没有人在使用它!”并删除ontouchstart
处理程序。在一瞬间,你的代码对任何人都不起作用,无论是触摸还是鼠标
TL;DR使用多个on()
调用没有错
我希望这能有所帮助。Famo.us中的on()
方法只接受一种事件类型。它不做任何jQuery风格的字符串处理来确定单独的事件。理论上,您可以有一个名为“到达斩波器”的事件
但是,在创建自定义视图时,我要做的是创建一个bindEvents()
函数,将所有事件侦听器分组在一起。这些处理程序中唯一的代码是其他函数。如果我想以相同的方式对两个不同的事件做出反应,我会对它们使用相同的函数
// An example Class demonstrating events - some off-topic parts omitted
function myLovelyHorseButton() {
// Recomended Reading: https://famo.us/guides/dev/events.html
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
this._eventInput.pipe(this.sync);
this.sync.pipe(this._eventInput);
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
/** Set options & variables
/* ...
*/
// create event listeners for this specific instance
_bindEvents.call(this);
};
// bind all the events for the button
function _bindEvents() {
//Call event handlers with this set to owner.
this._eventInput.bindThis(this);
// listen for events
this._eventInput.on('click', _handleClick);
this._eventInput.on('touchstart', _handleClick); //Same as 'click'
};
// Nay to the console on click/touch
function _handleClick(event) {
console.log('Nayyyyy!!!');
};
// create an instance of myLovelyHorseButton
var button = new myLovelyHorseButton;
// We would then add the button to the Render Tree
mainContext.add(button);
您不想使用当前使用的链接模式有一个很大的原因。这是因为通过在点击时发出一个touchstart事件,您可以推断出将有一些代码对其进行操作。有一天你可能会喝醉,决定“触摸设备上没有人在使用它!”并删除ontouchstart
处理程序。在一瞬间,你的代码对任何人都不起作用,无论是触摸还是鼠标
TL;DR使用多个on()
调用没有错
我希望这能有所帮助。Famo.us中的on()
方法只接受一种事件类型。它不做任何jQuery风格的字符串处理来确定单独的事件。理论上,您可以有一个名为“到达斩波器”的事件
但是,在创建自定义视图时,我要做的是创建一个bindEvents()
函数,将所有事件侦听器分组在一起。这些处理程序中唯一的代码是其他函数。如果我想以相同的方式对两个不同的事件做出反应,我会对它们使用相同的函数
// An example Class demonstrating events - some off-topic parts omitted
function myLovelyHorseButton() {
// Recomended Reading: https://famo.us/guides/dev/events.html
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
this._eventInput.pipe(this.sync);
this.sync.pipe(this._eventInput);
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
/** Set options & variables
/* ...
*/
// create event listeners for this specific instance
_bindEvents.call(this);
};
// bind all the events for the button
function _bindEvents() {
//Call event handlers with this set to owner.
this._eventInput.bindThis(this);
// listen for events
this._eventInput.on('click', _handleClick);
this._eventInput.on('touchstart', _handleClick); //Same as 'click'
};
// Nay to the console on click/touch
function _handleClick(event) {
console.log('Nayyyyy!!!');
};
// create an instance of myLovelyHorseButton
var button = new myLovelyHorseButton;
// We would then add the button to the Render Tree
mainContext.add(button);
您不想使用当前使用的链接模式有一个很大的原因。这是因为通过在点击时发出一个touchstart事件,您可以推断出将有一些代码对其进行操作。有一天你可能会喝醉,决定“触摸设备上没有人在使用它!”并删除ontouchstart
处理程序。在一瞬间,你的代码对任何人都不起作用,无论是触摸还是鼠标
TL;DR使用多个on()
调用没有错
我希望这会有所帮助。[EDIT]要以相同的方式处理单击和touchstart->touchend,只需使用FastClick覆盖垫片 只需添加:
FastClick = require('famous/inputs/FastClick');
这也适用于平板电脑:
anElement.on("click", function(){alert("Click caught")})
另一种选择是使用此帮助程序:
function onEvent(source, events, callback)
{
for (var event in events)
{
source.on(events[event], function(e) {
callback(e);
});
}
}
然后:
[编辑]要以相同的方式处理单击和touchstart->touchend,只需使用FastClick覆盖垫片 只需添加:
FastClick = require('famous/inputs/FastClick');
这也适用于平板电脑:
anElement.on("click", function(){alert("Click caught")})
另一种选择是使用此帮助程序:
function onEvent(source, events, callback)
{
for (var event in events)
{
source.on(events[event], function(e) {
callback(e);
});
}
}
然后:
[编辑]要以相同的方式处理单击和touchstart->touchend,只需使用FastClick覆盖垫片 只需添加:
FastClick = require('famous/inputs/FastClick');
这也适用于平板电脑:
anElement.on("click", function(){alert("Click caught")})
另一种选择是使用此帮助程序:
function onEvent(source, events, callback)
{
for (var event in events)
{
source.on(events[event], function(e) {
callback(e);
});
}
}
然后:
[编辑]要以相同的方式处理单击和touchstart->touchend,只需使用FastClick覆盖垫片 只需添加:
FastClick = require('famous/inputs/FastClick');
这也适用于平板电脑:
anElement.on("click", function(){alert("Click caught")})
另一种选择是使用此帮助程序:
function onEvent(source, events, callback)
{
for (var event in events)
{
source.on(events[event], function(e) {
callback(e);
});
}
}
然后:
这有帮助,但你能帮我吗