FlowRouter triggersExit - How to calling a template helper function



How do I call a function in a template from the router.js code?

In my router.js I have the following…

FlowRouter.route('/editProduct/:_id', {
                  action: function () { 
                                BlazeLayout.render("appLayout", {content: "editProduct" }); 
                  triggersExit: [ autoSave ] 

function autoSave(){

This works ok, but I want to replace the alert with a call to an autoSave helper function inside the editContent template code…

function autoSave(){

Is this sensible? and if so how do I define the autoSave in the template?



Sounds like a job for the onDestroyed callback. I would refrain from doing templatey stuff in the Router. Template lifecycle callbacks are great for clean up and initializations of templates.



Yes, therefore my solution was to create a function in the Template:-

function saveData(template) {
             // Read values from form fields, ID stored in onRendered
            doc = new Object;
            doc.title = template.find('#docTitle').value;
            Meteor.call( 'updateProduct', ID, doc);

Then call this from the onDestroyed()…

Template.onDestroyed( function( ) {
      saveData( this );

Seems to work ok.