【问题标题】:Call another function or return 2 result调用另一个函数或返回 2 个结果
【发布时间】:2015-09-27 05:28:34
【问题描述】:

所以我有下面的代码。第一个从会话中读取过滤器数据并返回汽车列表。该结果的第二个总汽车数量。显然,代码几乎是重复的。我可以合并它们并返回汽车对象和汽车数量吗?或者至少我可以从 total_car 函数内部调用 car 函数?

Template.list.helpers({

    car: function() {

        var filter_str = {}

        if (Session.get('manufactured_year_min')) {
            filter_str.manufactured_year = {$gt: parseInt(Session.get('manufactured_year_min'))};
        }
        return Car.find(filter_str);
    },

    total_car: function() {
        var filter_str = {}

        if (Session.get('manufactured_year_min')) {
            filter_str.manufactured_year = {$gt: parseInt(Session.get('manufactured_year_min'))};
        }
        return Car.find(filter_str).count();
    }

});

【问题讨论】:

    标签: templates meteor helpers


    【解决方案1】:
    Template.list.helpers({
    
        cars: function() {
            var filter_str = {}
            if (Session.get('manufactured_year_min')) {
                filter_str.manufactured_year = {$gt: parseInt(Session.get('manufactured_year_min'))};
            }
            var car = Car.find(filter_str);
            return {car: car, count: car.count()};
        }
    

    然后在您的模板中根据需要引用{{cars.car}}{{cars.count}}

    【讨论】:

    • 嗯,我有点跑题了。使用 Session 保存过滤数据是好习惯吗?
    • 使用 Session 变量来存储过滤器的内容,该过滤器可能会在页面上显示为输入字段,这是一种非常常见的模式,因为它使您的过滤器具有反应性。使用反应变量是另一种方法。
    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多