-
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
define observe handlers in Ractive.extend() #405
Comments
+1 |
+1 from me as well. Going to give it a post-0.4.0 milestone as I'm currently trying to wrangle the issue list down to the last few bugs standing in between us and the 0.4.0 release. |
Lurker here, but wanted to quickly ask: Would this default to init: false or true? As an aside, I think the current default to true is unexpected. No other event system that I have used defaults to firing upon instantiation. |
@briancray default to
|
@briancray I actually started implementing default observe handlers (along with default event handlers - #360) recently, and came to the view that it might not be as good an idea as it first seemed. The questions it raises about inheritance (from one component to another that extends from it) and so on get surprisingly hairy. The issue about In #360, there was some good discussion about whether event handlers on a child component should override handlers for the same event on a parent component, or whether you should be able to call So for all these reasons my enthusiasm for this and #360 have dampened over time. As for the With pub/sub based systems it's not uncommon to see stuff like this: // set up some form of data-binding...
model.on( 'change:foo', function ( foo ) {
view.renderFoo( foo );
});
// ...and initialise to the current value
view.renderFoo( model.get( 'foo' ) ); Observers avoid that kind of redundancy (and because of their clearer intent, it's easier to guard against things like circularity). But of course it's sometimes desirable to use observers in other ways, which is why the |
"It's designed to enable a form of reactive programming, where any given input state can lead to exactly one output state." Doesn't data binding achieve this alone? If pure data binding was enough, then Correct me if I'm wrong but maybe I understand after I've thought about it a little more: You've designed If it is intended to function that way though, I'd recommend a change to your documentation at http://docs.ractivejs.org/latest/ractive-observe. Right now there isn't any mention of how it affects data flow (can you return a new |
Thanks @briancray.
Yes, I agree. Unfortunately pure data binding isn't enough - there are occasions when you need to do something in a more imperative fashion, and var ul = ractive.find( 'ul' );
ractive.observe( 'listItems', function () {
this.set( 'listHeight', ul.offsetHeight );
}, { defer: true }); // defer until after DOM updates so offsetHeight is correct Another example - you might have a visualisation with thousands of points. In that situation, data-binding might be a) unnecessary and b) prohibitively expensive, so you drop out of the abstraction and construct the DOM manually. But you still want the visualisation to respond to application state: ractive.observe( 'selected', function ( newId, oldId ) {
if ( oldId ) {
$( '[data-id="' + oldId + '"]' ).removeClass( 'selected' );
}
if ( newId ) {
$( '[data-id="' + newId + '"]' ).addClass( 'selected' );
}
}); These are both forms of data binding, but ones that involve colouring outside the lines a bit. Speaking of 'outside the lines' there's a third possibility, which is that you have a Ractive component somewhere on the page (maybe a form component of some kind) and need to alter a different part of the page (which isn't controlled by Ractive, or belongs to a completely separate instance) reactively. In my experience the
Validation is one possible use, certainly, though not the original motivation. You have to call There's definitely overlap in what observers and computed values can do, though computed values aren't ideal for validation since you have to create The comment about the documentation is fair, it could be explained better, particularly as the |
Thanks for the responses Rich. And don't get me wrong, Ractive's API is simple to understand and I like the focus on eliminating the unexpected. This was just one area that caused some confusion for me until I read the documentation a few times. |
Same idea as #360, just extended to include observe as well. i.e., I would like to be able to write:
instead of
The text was updated successfully, but these errors were encountered: