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

Fire() event don't pass event argument. #1235

Closed
Iverson opened this issue Sep 19, 2014 · 5 comments
Closed

Fire() event don't pass event argument. #1235

Iverson opened this issue Sep 19, 2014 · 5 comments

Comments

@Iverson
Copy link

Iverson commented Sep 19, 2014

on-click="selectTab:{{i}}" will fire selectTab event with two arguments: event and i, but
ractive.fire('selectTab', i) fire same event only with single argument i. This is architecture bug.

Example - http://jsfiddle.net/5g3d912e/

Also if i pass method call like <a href="#" on-click="myMethod(i)">Link</a> then myMethod() will be called without event argument and it will be impossible call preventDefault() as result in my case window.location.hash will be changed. I think it makes sense to add option for passing event argument to method call.

@martypdx
Copy link
Contributor

@Iverson see #1172 for ongoing discussion on eventing syntax.

You can pass the event using the method syntax by including it explicitly using event: <a href="#" on-click="myMethod(event, i)">Link</a>

Also, you can return false from ractive event to auto-cancel, i.e. preventDefault stopPropagation

@evs-chris
Copy link
Contributor

@martypdx I think the goal here is to have a DOM-triggered event and a manually fired (from script, not template) event receive the same arguments. Having to check arguments length to determine how the event was fired is a little strange.

@Iverson There's not really anything meaningful that ractive could supply for an event object for script-fired events. In practice, manually fired events and DOM-fired events don't usually overlap much. My solution thus far has been to simply pass an empty object or check arguments in the handler when they do overlap. ractive.fire('selectTab', {}, 2)

@Iverson
Copy link
Author

Iverson commented Sep 19, 2014

@martypdx thanks, but return false helps only for event callback, if method call preventDefault not happens.

@martypdx
Copy link
Contributor

@evs-chris:

the goal here is to have a DOM-triggered event and a manually fired (from script, not template) event receive the same arguments. Having to check arguments length to determine how the event was fired is a little strange.

I don't think the current model is conducive to that. As you point out the proxy-event has an auto included event argument and calling fire doesn't have a logical equivalent, unless you're forwarding an event.

This is why I like, even though it's a bit more syntax, requiring the controller method fire to be explicitly provided in the template API (see my last comment on #1172 for my preference on syntax). Then, as a library, ractive can allow the developer to use events in the manner they most want to work as the event parameter is in their control

// developer decides to include event:
<button on-click='fire:select, {{event}}, {{i}}'>
// or not:
<button on-click='fire:select, {{i}}'>
this.fire('select', event, index)
// or
this.fire('select', index)

@Iverson:

but return false helps only for event callback, if method call preventDefault not happens

Yes, there are some features with proxy-events that have not been carried forward to method calls. Auto event subscription is another one. Checkout out the discussion on other thread and weigh in with your opinion.

@evs-chris
Copy link
Contributor

this.event is available to method call events. So, while it can mess with your API a little to use it, it is available.

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

3 participants