[SOLVED] Run helper with args depending of events


#1

Hi there :slight_smile:

I’m actually trying to re-run a helper depending on an event i’ve setted up. I have a collection of tasks that i display on my web page.

I already have events in my JS file, perhaps i dont know how to link events and helpers.

I also have a search input, with a submit button. On submit, i want to re-run my helper with some parameters. How could i achieve this ?

Here’s my actual code :

// LOAD DATA ON TEMPLATES 
Template.crmTreeView.helpers({
    pipelines: () => {
        return Pipelines.find({}, { limit: 10 });
    },
    collection_key: () => {
        var filters = [];
        for (var key in Pipelines.findOne({})) {
            filters.push(key);
        }
        return filters;
    }
});

i want to do something like this on button click to update displayed data :

// LOAD DATA ON TEMPLATES 
Template.crmTreeView.helpers({
    pipelines: (arg) => {
        return Pipelines.find({ my_key: arg }, { limit: 10 });
    },
    collection_key: () => {
        var filters = [];
        for (var key in Pipelines.findOne({})) {
            filters.push(key);
        }
        return filters;
    }
});

Any ideas ? Thanks a lot in advance ! :slight_smile:


#2

You can not directly trigger helpers via UI events, but you can have your event handler set a reactive variable that the template helper also uses. Something like

Template.crmTreeView.onCreated(function(){
 const instance = this;
 instance.my_key = new ReactiveVar({});
});
Template.crmTreeView.events({
 'click .element'(event, instance){
  instance.my_key.set(event.currentTarget.value);
 },
});
Template.crmTreeView.helpers({
    pipelines: (arg) => {
        let arg = Template.instance().my_key.get();
        return Pipelines.find({ my_key: arg }, { limit: 10 });
    },
});

#3

Ok i see what i have to do so, thanks for all ! :+1:


#4

Okay, so what u showed me before is perfectly working, i achieved what i wanted to do.

Perhaps now, with this, i’m trying to build a function which will determinate which operator i should use for my filter. Actually, i’m doing something like this :


filter_operator: function (operator, filter, val) {
        // console.log('hello world : ' + operator);
        var complete_filter = {};
        switch (operator) {
            case "equalTo":
                complete_filter = {[filter]: { $eq: val}};
                break;
            case "isDifferentFrom":
                complete_filter = {[filter]: { $ne: val}};
                break
            case "contain":
                complete_filter = {[filter]: { $regex: '.*' + val + '.*' }};
                break
// NOT DONE YET ///////////////
            case "doNotContain":
                complete_filter = {[filter]: { $eq: val}};
                break
            case "isSet":
                complete_filter = {[filter]: { $eq: val}};
                break
            case "isNotSet":
                complete_filter = {[filter]: { $eq: val}};
                break
// NOT DONE YET ///////////////
            default:
                break;
        }
        console.log(operator);
        console.log(complete_filter);
        return complete_filter;
    },

This function is défined into Meteor.methods, i’m able to call her at the moment.

Perhaps, i’m not sure about the operators. It’s actually apparently doing something, but there is no collection data displayed when it should be.

I, for example, have a record with a “label” that contains Test 1. Without this function, it was perfectly working. Where is the mistake ?

Here is the output of console.log(complete_filter) :

{label: {…}}
label: {$regex: ".*Test.*"}

It seems to be ok to me ?

Here is my new event :

'click .tw-filter-submit': function (events, template) {
        // swal("Ooops !", "This function is not available yet !", "info");
        var filterOperator = $('#crmFilterOperator').val();
        var selectFilter = $("#crmFilterSelect").val();
        var filterVal = $(".tw-filter-input").val();
        Template.instance().filtersVar.set(Meteor.call('filter_operator', filterOperator, selectFilter, filterVal))
    },

Thanks a lot in advance !

PS : If needed, here’s the rest of the JS :

// SUBSCRIBE TO PIPELINES PUBLICATIONS ON TEMPLATES
Template.crmTreeView.onCreated(function () {
    var self = this;
    self.autorun(function () {
        self.subscribe('Pipelines');
        var filters = [];
        for (var key in Pipelines.findOne({})) {
            // console.log(key);
            filters.push(key);
        }
        console.log(filters);
    });

    if (FlowRouter.getParam('page')) {
        var page = FlowRouter.getParam('page');
    }

    // Here we build the instance to set variables
    const instance = this;
    // This var will allow us to use filters on collection, with events and helpers
    instance.filtersVar = new ReactiveVar({});
});

// LOAD DATA ON TEMPLATES 
Template.crmTreeView.helpers({
    pipelines: () => {
        // here we return data according on our filters. If no filter, filtersVar is an empty object
        // so we will get all the data
        return Pipelines.find(Template.instance().filtersVar.get(), { limit: 10 });
    },
    collection_key: () => {
        var filters = [];
        for (var key in Pipelines.findOne({})) {
            filters.push(key);
        }
        return filters;
    }
});


#5

I don’t think your autorun in the onCreated will execute more than once, because there is not reactive source that will trigger the autorun


#6

Oh that’s true. It’s an old dev on which i’ve set the Pipelines subscription, and i tried to display the keys of my collections inside just for some dev research. Do you think it is related with the fact the operators aren’t working ?

Thanks a lot for your support,


#7

Could be related. I strongly recommend you read the Meteor Guide and Blaze instructions


#8

Okay, i’ll check it again to see what i may missed. Thanks !


#9

Well, i found the solution.

It wasn’t related to the autorun (Perhaps it was still false in another way as you said). For those who would may be interested :

The meteor call command i used to get the “complete_filter” do not return the value i tried to return.

I got to EXPORT this function (i did it in another file) en then import her at the top of the file. Then it was perfectly working.

Topic closed :slight_smile: