by bryanlarsen · 13y ago · 0 comments
Here's a typical ViewModel definition (in JS):
foo.vm.Resource = highbrow.ViewModel.extend({
lastUpdated: function() {
return moment(this.model.get('modifiedAt')).fromNow();
}
});
foo.vm.Resource.attrs(['name', 'content']);
This shows the main two features of ViewModel's: a whitelist of attrs that the template can access directly, and a set of functions that would otherwise end up polluting your model with view side code.This decoupling dramatically improves the structure of our applications, without introducing Rails style controllers which are also totally misnamed in my opinion. Rails controllers are essentially two things: routers and MV-shims. Splitting the two makes for much improved program structure.
1: https://github.com/airbnb/rendr/blob/master/shared/base/view...
2: https://github.com/wvl/highbrow/blob/master/src/view-model.c...