Skip to content

Instantly share code, notes, and snippets.

@marcodejongh
Last active August 29, 2015 14:01
Show Gist options
  • Save marcodejongh/c2056ab5b5f7a360cb0a to your computer and use it in GitHub Desktop.
Save marcodejongh/c2056ab5b5f7a360cb0a to your computer and use it in GitHub Desktop.
var formCollection = new Meteor.Collection(null);
var itemId = 'SEDatkPtyfAKvGgpj';
Template.form.rendered = function () {
};
Template.form.changed = function () {
var form = formCollection.findOne(itemId);
return !collections.wave.find(form).count();r
}
Template.form.created = function () {
};
Template.form.data = function () {
var ready = Meteor.subscribe('wave').ready(),
form;
if(ready) {
form = formCollection.findOne(itemId) || {};
if (!_.size(form)) { //No form exists yet
var wave = collections.wave.findOne(itemId);
if(!wave) {
router.go(currentRoute, {itemId: formCollection.insert(collections.wave.template())});
}
form = formCollection.findOne(formCollection.insert(wave));
}
}
return form;
};
Template.form.destroyed = function () {
};
Template.form.events({
'submit': function (event, template) {
event.preventDefault();
var modifier = {};
modifier['$set'] = {};
$(template.findAll('input')).each(function () {
var field = $(this);
modifier['$set'][field.attr('name')] = field.val();
});
formCollection.update(this._id, modifier);
collections.wave.update(itemId, formCollection.findOne(itemId));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment