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

Allow syncWith() to specify interfaces #21

Open dbushong opened 12 years ago

dbushong commented 12 years ago

Currently syncWith() takes no options, AFAICT. (Contradicting the docs, BTW)

It would be nice to be able to bidirectionally link two objects that don't share the same interface. Unfortunately, the obvious method for doing this, passing an object with observerInterface and subjectInterface wouldn't help because they switch roles. However, it's entirely possible (likely?) that the interface for each specific object would stay the same for both directions, we just need a way of specifying them, but I'm not sure the best way to do that.. leftInterface and rightInterface ? Blech.

Example:

var a = Synapse($('#foo')), b = Synapse(new Backbone.Model());

// tedious
a.observe(b, 'some_prop_on_b', 'attr.some_attr_on_a')
  .notify(b, 'attr.some_attr_on_a', 'some_prop_on_b');

// better?
a.syncWith(b, { leftInterface: 'attr.some_attr_on_a', rightInterface: 'some_prop_on_b' });

// a.k.a.
a.syncWith(b, 'attr.some_attr_on_a', 'some_prop_on_b');

Probably also helpful to have a way to specify triggerOnBind with a direction in the options (i.e. which object is the source of authority for the initial one-way sync)

bruth commented 12 years ago

I too have not come to a clear/clean solution for this. It most likely will need to be denoted by left and right. When interfaces and/or events are not detected, the verbosity of defining a binding is miserable. An alternate approach for configuring the DOM-based hooks (jQuery, Zepto, and Backbone View) is to extend these: https://github.com/bruth/synapse/blob/master/src/synapse/hooks/jquery.coffee#L206-231 which are exposed, but not documented.