-
Notifications
You must be signed in to change notification settings - Fork 396
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
Method calls #1146
Method calls #1146
Conversation
Adding those array modification methods to |
How sweet is this: <input type='checkbox' checked="{{all}}" on-change="set('todos.*.done', all)"> UPDATE: Oh, don't even need the |
I like it! My only concern would be discoverability of the event object. Is it always just |
Should |
Yep!
That's a fair point. I think (can't check right now) that you can work around it by using
I did wonder about that... it would mean that there was no way to access custom event properties (e.g. a |
Nah, it's a bad idea. I'll come up with some <input on-enter='push("todos", { description: newItem, done: false }).then(set(newItem, ""))' value='{{newItem}}'>
// or maybe just
<input on-enter='push("todos", { description: newItem, done: false }); set(newItem, "")' value='{{newItem}}'> Couple of issues thus far: on-mousemove='' // empty handlers fall over
on-mouseup="{{#condition}}set('foo', bar){{/}}" //blocks don't work Also, IMO we should keep the existing syntax as well. It's a different set of use cases around publishing component events. |
I think the old syntax should be deprecated. It'd be confusing for new users to have 2 ways of doing almost the same thing and it's still possible to use |
@MartinKolarik for the case when you do want to pub an event, IMO it sucks to go from: |
What about a third option - retain <widget on-foo='bar:"these arguments will be ignored"'/>
<!-- widget template -->
<button on-click='foo:1,2,3'>click me</button> Maybe allowing For the record my inclination is the same as @MartinKolarik's :-) |
That makes sense as you could then allow params on the component defined by the parent: {{#items:i}}
// these actually make sense!
<widget on-foo='bar:"valid args", {{i}}'/>
{{/}}
<!-- widget template -->
// yes :)
<button on-click='foo:'>click me</button>
// no :(
<button on-click='foo:"parse error?"'>click me</button>
Yep |
As a newcomer to Ractive, the concept of proxy events and all their benefits is a somewhat unique feature and major draw to the library. Complicating that syntax with As verbose as multiple |
@thomsbg Definitely agree that having a way to fire events in a way that's 'consumer-agnostic' is important - my hunch though is that the vast majority of uses for events are better met by calling methods directly. Personally, my app code is littered with things like... ractive.on( 'select', function ( event, selected ) {
this.set( 'selected', selected );
}); ...where there's really no benefit to treating it as an event. I actually find it very rare that events make more sense (outside the context of components, where it's necessary to listen to child component events), but I'd be interested in any examples you have where events are necessary and In any case, both syntaxes will be supported from 0.5.6, and if we do deprecate it it'll be after we've thoroughly discussed the pros and cons. In the meantime, I'll merge this PR - am excited about this one, it's going to save us all a ton of code. |
👍 |
👍
With bubbling events you might very well want the component event to have args |
Adding my contribution to the discussion. We're building a rather large dashboard, full-ractive (multiple rewrites, each making more and more use of it) and we rarely have single line event handler methods as opposed to you @richharris - most are doing a few operations to massage data - different coding styles I guess. I'm also worried about crossing a conceptual line here: as much as I am at peace with adding logic to the views with expressions, because it simplifies code that much, I'm troubled by adding state-mutating code directly there. Reminds me of this ugly inline JavaScript we had when DHTML became a trend. I'm also worried about discoverability: on larger apps, having two places for each component where data can be modified seems like a recipe for headaches 2 month down the road Again that's my 2 cents. Have to validate those assumptions ;) |
Now that this issue has been merged, it's probably better to continue the discussion about deprecating the old syntax on a separate thread, otherwise this one risks getting buried. I've just opened #1172 for that purpose. |
This pull request is in the same spirit as #1141 - it's a work-in-progress (e.g. no tests/error handling!), put here in hopes of stimulating discussion and catching any howlers before it gets merged in. Like
{{yield}}
, this is a feature that I'd sort of envisaged as part of the next major release (0.6.0 - not semver major, but whatever), but there's no harm in getting it out there sooner so we can sweep up any bugs.For detailed background, see #568 (from this comment onwards). The idea is to replace (eventually - this PR doesn't break any existing code) the current event syntax. Instead of...
...we can do this instead:
It could be any method belonging to the current instance, even
teardown()
:If you need to access the
event
object, you can:I didn't fully realise how powerful this pattern is at first. Here's an 80% complete TodoMVC implementation (we're using the key event plugin for
on-enter
):You can still fire 'proxy events' to achieve the same effect as the old syntax...
...but the need to fire events like this will be much less frequent. (In fact, I did wonder about rewriting the existing event handler code so that
select:{{object}}
is automatically treated asfire("select",event,object)
at parse time, to make the runtime code a bit leaner. But there are a couple of edge cases where rewriting gets tricky. Never mind, if we decide to deprecate the old syntax, we'll be able to delete that extra code soon enough.)So...
As I say this is a WIP but I hope to merge it in soon. If anyone has any strong objections, speak now or forever hold your peace! The only major outstanding question is whether the old syntax should be deprecated over the next few versions - my vote is yes (it's less intuitive, more verbose in most cases, has more edge cases, and is a maintenance burden).
If you want to try it out in the meantime, here are some steps (should probably come up with a way to host feature builds online...):
Then, copy the
sample
folder insandbox
, and poke around with theindex.html
file therein.