@manuel how does ViewModel work with models returned from collection finders? Do you have any examples combining the two. Your current examples only show essentially static models, in other words “view models” lol.
For example we want to do this:
<template name="Parent">
{{#each posts}}
{{> PostViewModel}}
{{/each}}
</template>
<template name="PostViewModel">
<h1>{{title}}</h1>
<p>{{body}}</p>
</template>
Template.MyViewModel.viewmodel({
//what goes here?
//specifically what enhnacements could go here for dealing with dynamic data/collections.
});
Perhaps to get the real benefits we need to be working with forms and event handlers??
<template name="EditPostViewModel">
<input value="{{title}}" />
<textarea>{{body}}</textarea>
<button>SAVE</butt>
</template>
Template.MyViewModel.viewmodel({
'click button': function() {
//basically, you're reaching into the DOM via the old approach here:
this.save({title: $('input').val(), body: $('textarea').val()); //our models have a `save()` method obviously
//ideally it would be something like this:
this.populate().save();
//or simply:
this.saveFromForm();
//or perhaps yours looks closer to something like this:
this.save({title: this.title() body: this.body()}); //though the `title` method conflicts with a prop of the same key
//so maybe this:
this.save({title: this.viewmodel.title() body: this.viewmodel.body()});
//or:
this.model().save({title: this.title(), body: this.body()});
//or:
this.save({title: Template.viewmodel().title(), body: Template.viewmodel().body()});
}
});
So basically the goal is for your automatic bindings to continue on to true models. The above event handler is full of contrived examples obviously–what’s the best practices way to accomplish that with ViewModel 2? I guess the real question would be how to make the bindings for methods such as this.title()
to the true model. And is having to make those bindings really that much of an improvement over giving your model a method to suck values out of AutoForm inputs on the page?
The following seems a lot simpler for the reality where when you’re dealing with inputs it’s 85% of the time for collection models:
this.populate('formName').save(); //formName is optional; if not provided, `populate()` will find the first AutoForm on the page
and if you only wanna do one field:
this.populateField('title').save();
Obviously it’s assuming you’re using something like AutoForm and built the simple populate()
method into your model class, which couples the model nicely with AutoForm.
You may say populate()
is extra work. But for any medium-level application in complexity, it’s a necessary evil. It’s a feature rather. Why you may ask? Because who says you want to save whatever the user enters all the way to the database?? You very often need the old values on the model to compare, or perform various options (perhaps use the old value, if the new value doesn’t suffice). So serious applications don’t want to just take what’s in the form without the option to maintain the old data. In short, populate()
is a necessary “evil,” and a 10 character evil at that–so it’s no big deal.
I’m having a hard time seeing how ViewModel isn’t for anything but the most trivial of use cases where all the state is purely client side, or rather, when all the state is client side and doesn’t deal with collections (you can have client side only collections obviously). It just seems like any gains you would have gotten from ViewModel you spend marshalling data back and forth from a true model to a view model. What you lose by using a true model instead of ViewModel you make up with simple features like populate and the popular AutoForm package. Overall, it’s simpler, but perhaps more importantly, it’s more standards-based, i.e. what developers are already used to. They don’t have to learn a specialized system. I’m not against learning specialized systems, but it needs to come at substantial gains.
That all said, I love your package, tell me what I’m missing because surely there must be a way in ViewModel that trumps the model-based AutoForm standard solution I provided.