How the create an event handler similar to Bangle.on("step"...........) #2003
Replies: 14 comments
-
Posted at 2020-07-28 by d3nd3-o0 There are some examples. I recommend that you download/clone the BangleApps repo, https://github.com/espruino/BangleApps. And use a decent IDE/text editor that can search through all the projects in there. https://github.com/espruino/BangleApps/blob/master/apps/jbells/jbells.js From briefly looking at the examples, its enough to call
and later
The "Object" class is a compatible EventEmitter by default. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr Thanks for the tip, why didn’t I think of that. Have been searching the doc and forum for some clues how to do, but found nothing. Would be great for others if a simple example is added to the doc, which is by the way excellent. Will try to make a working example today and post it here. Cheers. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by @MaBecker There are lots of details on the reference page eg object class where you find .emit() and .on(). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr Hi Marck, Yes, I saw the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by @gfwilliams Would code examples added to http://www.espruino.com/Reference#l_Object_on and http://www.espruino.com/Reference#l_Object_emit help at all? That would be an easy change in a reasonably obvious place... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr Based on the tip from @d3nd3-o0 I changed the example in message #1 to:
The result: So that works! So, there is no need in Espruino to Thanks to @d3nd3-o0 for guiding me in the right direction! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr @gordon. YES, a simple example like in my previous post would have helped me getting this working. And YES, adding such an example to the doc for on/emit is the perfect place. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by @gfwilliams Great - just added: espruino/Espruino@180b4e4 |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by @MaBecker So have fun and use this forum for further questions, you will find many members that are willing to support others. But keep in mind if you are not using original boards you should send a donation to Gordon. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr Thanks for the nice example, would have saved many hours of my life ;-) My apologies for being thick, but the example on Github will automagically appear in the Espruino Reference, right? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by gerardwr Thanks for the "nudge", DONE! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-29 by @allObjects @gfwilliams, the names used in the brief explanation / example is the issue... 'We' who are used to objects and the event emissioning and listening (as for example NodeJS has implemented it), the frugal examples are 'understood' / 'interpreted' the right way, last but not least the names are used all over the place in such environments and type and purpose of their elements/arguments are clear to us:
Just adding some comments already helps:
There would be more to say, but your changes in doc make it understandable, and also the link to node js doc helps. A hint about what to search for would be even more helpful: Google / Search the web for 'node js emit on' |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-30 by @gfwilliams Ok - I've just added some comments :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-31 by gerardwr This is a working example I have come up with with your help, maybe it's of any use to others.
Output of the script when the button is pressed: In this example the init function needs to be called at start of the main script. Not sure if there is a way to move this init into the Object, but this works for me. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2020-07-28 by gerardwr
Hi,
I would like to create my own event handlers similar like the Bangle event handlers, f.i. the "step" handler.
Tried this example from https://www.w3schools.com/nodejs/nodejs_events.asp
But this results in the error:
I read in the Espruino documentation here http://www.espruino.com/Reference#l_Object_emit about Object.emit and Object.on but can not find any reference or example on how to setup an eventEmitter in Espruino.
I'm rather new to Espruino/Javascript so maybe there's an obvious answer. Can someone point me in the right direction?
Beta Was this translation helpful? Give feedback.
All reactions