mortensen
mortensen

Reputation: 1237

Define autoform hook for forms in for each in Meteor

I have a for each loop in Meteor and I'm using autoform to update each item (just like described at http://autoform.meteor.com/update-each).

My problem is that I normally create notifications through hooks with

AutoForm.hooks({
  myFormId: {
    onSuccess: function(formType, result) {
      Notifications.success('Title', 'Text.');
    }
  }
});

but since all my forms have unique IDs, I cannot use this. How can I create a hook which matches all forms in a template or has a name which matches a regular expression "unique-id-?" where ? is the docId?

Upvotes: 1

Views: 201

Answers (1)

Matthias A. Eckhart
Matthias A. Eckhart

Reputation: 5156

This may not be the optimal solution, but it works:

Template["updateEach"].helpers({
  items: function () {
    return Items.find({}, {sort: {name: 1}});
  },
  makeUniqueID: function () {
    return "update-each-" + this._id;
  }
});

Template.updateEach.onRendered(function () {
  var hooksObject = {
    onSuccess: function (formType, result) {
      Notifications.success('Title', 'Text.');
    }
  };
  var formIds = Items.find().map(function (item) {
    return "update-each-" + item._id;
  });
  AutoForm.addHooks(formIds, hooksObject);
});

Upvotes: 1

Related Questions