Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.on('*') is possible, but there's no way to get event name #1249

Closed
rstacruz opened this issue Sep 24, 2014 · 9 comments
Closed

.on('*') is possible, but there's no way to get event name #1249

rstacruz opened this issue Sep 24, 2014 · 9 comments

Comments

@rstacruz
Copy link
Contributor

You can do this:

ractive.on('*', function () {
  console.log("* event fired:", arguments);
});

But this will only get you the event's arguments, but not the event name:

ractive.set('hello', 'world');
/* event fired: [ { hello: world } ] */
/* ...this is a `change` event, but there's no way to determine that. */

I'm not sure on what the best solution for this is etiher, but perhaps there should be a way to determine what was the last event fired.

@rstacruz rstacruz changed the title .on('*') is possible, but no way to get event name .on('*') is possible, but there's no way to get event name Sep 24, 2014
@Rich-Harris
Copy link
Member

Maybe this.event? Just like window.event is the last DOM event that fired (or d3.event, if that's a more palatable example...).

ractive.on('*', function () {
  console.log(this.event.name + " event fired:", arguments);

  // if it's a proxy event, would have all the usual stuff
  this.event.original.preventDefault();
  console.log( this.event.keypath );
});

To be honest, it should probably have been that way all along - then we wouldn't have the discrepancy between programmatically-fired events and proxy events (whose arguments have to be unshifted to accommodate the event object). (See also #1235.)

@rstacruz
Copy link
Contributor Author

That looks pretty neat!

@rstacruz
Copy link
Contributor Author

Btw, this'd make it possible for the Ractive adaptor to support events propagated from child to parent. The adaptor can listen for child.on('*') and propagate those events upwards.

@ryanramage
Copy link
Contributor

+1 for this.event proposal. We are looking to use the ractive-ractive adaptor for some large UI work.

@codler
Copy link
Member

codler commented Sep 25, 2014

Are ractive.on('*' documented?

@martypdx
Copy link
Contributor

@codler added with event bubbling, which I don't think has been added to the docs yet (mea culpa).

Leverages dot notation: *, *.foo, foo.*

@martypdx
Copy link
Contributor

#1263 Addresses this (but not the first arg event object deprecation, will do separate PR for that).

  • handler name added to event object as event.name
  • event object accessible via this.event in handlers
  • event created as this.event for fire() with name, context (ractive.data), keypath ('')

@evs-chris
Copy link
Contributor

@codler it's partially documented at http://docs.ractivejs.org/latest/components#events

@martypdx
Copy link
Contributor

Thanks @evs-chris!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants