ripplejs / ripple

A tiny foundation for building reactive views
http://ripplejs.github.io
1.28k stars 66 forks source link

Multiple views, singe data source? #28

Open benjamminf opened 10 years ago

benjamminf commented 10 years ago

Is there any way to use a central data source that multiple views make and watch for changes with?

I've toyed around with having models share observers but it fails when you have nested views (so when using the each plugin).

Also, brilliant work you've done with this framework! It's been very useful, so thank you.

anthonyshort commented 10 years ago

Hrmm that's tricky since the objects aren't observed, they rely on get/set. I eventually want to switch it to Object.observe so this will be easier.

For now, you might want to just pass in another type of model to the views and listen for changes manually.

var model = new Model();
model.set('foo', 'bar');

var View = ripple(template);
View.on('created', function(view){
   view.data.model.on('change foo', function(val){
       view.set('foo', val);
   });
});

new View({
  data: {
    model: model
  }
});

It's not pretty at all. You'd just watch that object for changes and then set another property on the view. Then you could do this in both views.