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.
AutoForm.hooks uploadForm: before: insert: (doc, template) -> upload @, doc, file return
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) -> $.ajax url: "https://api.imgur.com/3/upload" success: (data, textStatus, jqXHR) -> doc.url = data.data.link doc.deletehash = data.data.deletehash promise.result doc