Why is no posible change value of Ex: Template.instance().data


#1

This´s a newbie question.
Why I can´t set data context to where ever object?

Ex:

Template.example.events({
'dblclick tbody > tr': function (event) {
    // Current rowData
    var dataTable = $(event.target).closest('table').DataTable();
    var rowData = dataTable.row(event.currentTarget).data();

    Template.instance().data = rowData;
    console.log('[Template.instance().data]: ', Template.instance().data);
}

On console instance data remain the same.


#2

Template.instance().data is a convenient way of getting to the data context of the current instance of the template within helpers, event handlers, callbacks and autoruns. From the documentation this is read only.


#3

There is a pattern that allows to use Template.instance() to communicate between events and helpers, though (here is an old meteorpad about this)

Basically you add a (reactive) variable to the template during creation, and then access that variable in your event handlers and helpers

Template.instance().data is readonly, however, just as @robfallows said.


#4

Thanks Rob.
I use this code.

DataTableExtra.events = {
    // Here we fired a custom event(dblclickrow) for past current rowData to template.rowData
    'dblclick tbody > tr': function (event, template) {
        // Current rowData
        var dataTable = $(event.target).closest('table').DataTable();
        var rowData = dataTable.row(event.currentTarget).data();
        template.rowData = rowData;
        $(event.currentTarget).trigger('dblclickrow');
    }
};

DataTableExtra.events is a shared event handler

Template['usuarios_table'].events(DataTableExtra.events);
Template['usuarios_table'].events({
    //dblclickrow a custom event on datatable-extra package
    'dblclickrow tbody > tr': function(event, template) {
        // code here
       console.log('[rowData dblclickrow]: ', template.rowData);
    }
});

#5

thanks @jamgold I know and use that pattern but I was trying to set data context on template instances who has useless this.
I ´m a newbie trying to behave like a “oldbie” :smile: