FlowRouter triggersExit - How to calling a template helper function


#1

Hi,

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(){
    alert('Here..')
}

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(){
    Template.editProduct.autoSave();
}

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

Thanks


#2

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.


#3

Thanks.

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.