Search code examples
meteormeteor-blaze

Meteor Blaze fetch data and update template


I have the following logic in my meteor app:

   Template.configs.events = {
        'click li': function(e, template) {
           Meteor.call('getMaster', uri, function(err, response) {
           // Template.set(response)
        });
        }
    };

After a user clicks on a list, a json object is returned via Ajax. How would I dynamically inject this object into the template? Is there a design pattern?

Thank you in advance.


Solution

  • you might handle such return data through the template's reactive variables. using your code, i wrote up a "full" example showing the initialization, setting, and getting of such vars:

    Template.configs.onCreated(function () {
        this.foo = new ReactiveVar();
        this.bar = new ReactiveVar();
    });
    
    Template.configs.helpers({
        foo() {
            return Template.instance().foo.get();
        },
    
        bar() {
            return Template.instance().bar.get();
        }
    });
    
    Template.configs.events({
        'click li': function(e, template) {
            Meteor.call('getMaster', uri, function(err, response) {
                let foo = response.foo;
                let bar = response.bar;
    
                template.foo.set(foo);
                template.bar.set(bar);
            });
        }
    });