component / reactive

Tiny reactive template engine
383 stars 48 forks source link

Cascading model changes to delegators? #132

Closed gnimmelf closed 10 years ago

gnimmelf commented 10 years ago

Given

var model = {first_name: 'First', last_name: 'Last'};
var view = reactive('<div>{name}</div>', model, {
  delegate: {
    name: function(){ 
      return model.first_name + ' ' + model.last_name;
    }
  }
});
document.querySelector('#app').appendChild(view.el);
view.set('last_name', 'NewLast');

How would you suggest going about to trigger an update to name? -Or maybe my approach is wrong...

defunctzombie commented 10 years ago

In this case you are gonna want to use some "model" library which has a concept of pseudo properties/synthetic properties/or computed properties (this has many names). That model library would then be able to emit an event indicating that name changed when you just change last_name and an adapter would pick that up.

Reactive doesn't support this out of the box to keep core simpler. Once you start having these types of "pseudo" properties you are gonna have to jump to some sort of model layer.

In this case I would just use first_name and last_name directly in the view template.

gnimmelf commented 10 years ago

Yup, contrived example, and was afraid of that =) Thanks.