Skip to content

Instantly share code, notes, and snippets.

@lukeclifton
Created November 13, 2014 11:46
Show Gist options
  • Save lukeclifton/bfaaa8cea70824f89c90 to your computer and use it in GitHub Desktop.
Save lukeclifton/bfaaa8cea70824f89c90 to your computer and use it in GitHub Desktop.
formsubmitter function
Template.customerCreate.rendered = function () {
$('.formsubmitter').click(function(e) {
e.preventDefault();
var form = '#' + $(this).data('form-id');
$(form).submit();
});
}
@olizilla
Copy link

It'd be idiomatic to add that as a template event map: http://docs.meteor.com/#/full/template_dynamic
rather than in a rendered. That doesn't solve you're problem of needing it to be global, but it's a start

@olizilla
Copy link

so the simples way that has some repitition is to:

function myFancySumbitHandler (e) { e.preventDefault() /* etc */ }

Template.customCreate.events({
  'click .formsubmitter':  myFancySumbitHandler
})

Template.otherThing.events({
  'click .formsubmitter':  myFancySumbitHandler
})

@lukeclifton
Copy link
Author

so theres no easy way to make this kind of code global? seems kind of a basic need

@olizilla
Copy link

of note, it's nicer to attach a submit event handler to a form element and then you catch both button clicks and enter button keypress, so people can submit the form how they prefer

@olizilla
Copy link

You want every form in your app to have the same submit handler? You should probably refactor it out to a common template and re-use the template.

@lukeclifton
Copy link
Author

yeah i know what your saying...the reason i'm using this formsumitter code is because my submit button is outside of my form for UI reasons

@lukeclifton
Copy link
Author

cheers for you help dude

@olizilla
Copy link

wait, what even is going on here? You're passing form id's in as template data, to pull them out later to know which form to submit? Sounds hella complex. You wanna chat?

@olizilla
Copy link

@richsilv
Copy link

Incidentally, although it may not end up being relevant here, I do think there is call for global rendered/created/destroyed callbacks and have made the case on meteor-talk in the past. Not sure whether anything will happen, but I do think that there is a use case for this (even if it isn't this one), at least as a set of convenience methods.

@lukeclifton
Copy link
Author

ok, permission to hit me. i realise now that i can just get the form id from the event handler. i pasted the code from a previous non meteor project and didn't check it.

@lukeclifton
Copy link
Author

but yes agree with @richsilv, global rendered functions is still a need

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment