Then the article flashed like a dude, choosing a tool, could not write anything. This is about me! On New Year's Eve found ViewModel.org . And inside is a beautiful Two-Way Binding . But the feeling of a box in a garage littered with wrenches interspersed with "extra" details does not go away. A good example of how in his own demo it was necessary to screw a crutch , gee-gee. And I was stuck for four months - a hobby in the evenings after work. Went through all the packages , though somehow useful for Blaze. Molested to the authors with pleas ... Collected in the end a new bike with a loud name Template2 .
It is very difficult to do just as you know. Check out:
<input value-bind>
attribute (which is separately useful for debugging the alien code). <body> {{> hello param="123"}} </body> <template name="hello"> <p>props.param {{props.param}}</p> <p>state.value {{state.value}}</p> <form> <input value-bind="value"/> <button type="submit">Submit</button> </form> <p>{{state.errorMessages}}</p> </template>
// - ! Template2('hello', { // , propsSchema: new SimpleSchema({ param: { type: String } }), // modelSchema: Posts.simpleSchema(), // states: {}, // , Template.instance() helpers: {}, events: {}, // , Blaze onCreated() {}, onRendered() {}, onDestroyed() {}, }); // , Template.instance() Template.hello.eventsByInstance({ 'submit form': function(e) { e.preventDefault(); // Get doc after clean and validation for save to model this.viewDoc(function(error, doc) { if (error) return; Posts.insert(doc); }); } }); // . Template.hello.onRendered(function() { var self = this; this.autorun(function() { var doc = Posts.findOne(); if (doc) { // Set doc from model to view self.modelDoc(doc); } }); });
Run the demo? Easy:
$ git clone https://github.com/comerc/meteor-template2.git $ cd meteor-template2 $ meteor
Source: https://habr.com/ru/post/283152/
All Articles