bruth / synapse

Hooks to support data binding between virtually any object.
http://bruth.github.com/synapse/docs/
BSD 2-Clause "Simplified" License
150 stars 6 forks source link

Implement unobserve-like API #12

Closed pgherveou closed 12 years ago

pgherveou commented 12 years ago

I see there is a offEventHandler in the hooks modules But is there any public api available at the moment to unobserve a Synapsed object ?

bruth commented 12 years ago

@pgherveou There is not currently. I am still figuring out what kind of unobserving there should be. I have been situations where I want to effectively block the channel while making changes to it and it unblocks when a user confirms the changes (e.g. via a save button). Otherwise changes would be happening automatically under the hood, but those changes may be unconfirmed.

What scenarios would you use this functionality? I realize it needs to exist, but I am just mulling over a proper implementation.

pgherveou commented 12 years ago

Well actually I didn't really need it in my last case But let's say you have a list of user and a detail view where you can edit user properties. You could use Synapse to bind the current model to the form, but every time you pick a user in a list you probably want to unbind current model before observing a new one. what do you think ?

bruth commented 12 years ago

Yep, that is a clear use case (if fact I have done that plenty of times before using jQuery..). Thanks.

bruth commented 12 years ago

Also.. please check out the new docs I have been writing up: http://bruth.github.com/synapse/docs/ They still need a lot of work, but it is much more clear as what is going on.

pgherveou commented 12 years ago

very nice, good job I let you know if I find some mistypings

bruth commented 12 years ago

So my first pass for an API looks like this:

There are equivalent methods for notifiers (e.g. stopNotifying). By default, these methods are applied to all of their corresponding objects. To target a specific object, simply pass in a Synapse object representing the target. A test from the suite.

var input = new Synapse('<input />');
var span = new Synapse('<span />');
var h1 = new Synapse('<h1 />');

span.observe(input);
h1.observe(input);

input.set('value', 'hello world!');
// mimic the event
input.raw.trigger('keyup');
// ensure the data propagated
equal(span.get('text'), 'hello world!');
// pause observing
span.pauseObserving();

input.set('value', 'foobar');
// mimic the event
input.raw.trigger('keyup');
// h1 still updates
equal(h1.get('text'), 'foobar');
equal(span.get('text'), 'hello world!');

// resume observing of all weak refs
span.resumeObserving();
input.pauseNotifying();

// mimic the event
input.raw.trigger('keyup');
// ensure the value still hasn't changed
equal(span.get('text'), 'hello world!');

input.resumeNotifying();

// mimic the event
input.raw.trigger('keyup');
// the value has now updated
equal(span.get('text'), 'foobar');

// detach all event handlers
span.stopObserving();
equal(input.raw.data('events'), undefined);
pgherveou commented 12 years ago

Looks good Waiting for the commits !

bruth commented 12 years ago

Implemented in 899e763c16