Clearing search results


#1

Hey everyone-

I have a template helper named appSearch. The user is able to select various criteria with checkboxes and the result will display a list of apps that match the criteria.

Would there be a simple way to “reset” the search, once filtered down, to display ALL available apps? I think I am confusing myself and perhaps am making this over-complex.

Thanks!

Template.AppList.events({
    'submit .searchForm': function(e, i) {

        let query = {};
        let subjectArray = [];
        _.each($('input[name="subject"]:checked'), function(elem) {
            subjectArray.push(elem.value);
        });
        query.subject = { $in: subjectArray };

        let ageArray = [];
        _.each($('input[name="age"]:checked'), function(elem) {
            ageArray.push(elem.value);
        });
        query.age = { $in: ageArray };

        let priceArray = [];
        _.each($('input[name="price"]:checked'), function(elem) {
            priceArray.push(elem.value);
        });
        query.price = { $in: priceArray };

        if (query.price.$in.length == 0) {
            delete query.price;
        } else {
            Session.set('price', query.price);
        };

        if (query.age.$in.length == 0) {
            delete query.age;
        } else {
            Session.set('age', query.age);
        };

        if (query.subject.$in.length == 0) {
            delete query.subject;
        } else {
            Session.set('subject', query.subject);
        };

        return false;
    },

    'click #resetSearch': function(e, i) {
        return Apps.find(null);
    }
});

Template.AppList.helpers({
    appSearch: function() {
        let query = {};

        query.age = Session.get('age');
        if (query.age == undefined) {
            delete query.age;
        };

        query.price = Session.get('price');
        if (query.price == undefined) {
            delete query.price;
        };

        query.subject = Session.get('subject');
        if (query.subject == undefined) {
            delete query.subject;
        };

        return Apps.find(query);

    }
});