Using async hooks in Meteors autoform

Fri Jul 18 2014

Useful feature for uploading a file through a form generated with autoform, or any other async functionality.

If you need to validate some form data in Meteor you are probably using the autoform package. If you want to do some custom actions before the form is submitted, you can do this through hooks. Hooks can be used asynchronously, which is very useful for something like waiting for image upload.

In your hooks section.

      insert: (doc, template) ->
        upload @, doc, file

Your upload function should call @.result() from the insert method, and pass the changed doc on success, or false if you want the upload to fail. Here is an example where the form waits for an ajax call to the imgur api.

upload = (promise, doc, file) ->
    url: ""
    success: (data, textStatus, jqXHR) ->
      doc.url =
      doc.deletehash =
      promise.result doc