如何筛选主干.js集合和重新呈现应用视图

How to filter Backbone.js Collection and Rerender App View?

本文关键字:集合 新呈现 视图 应用 js 何筛选 筛选      更新时间:2023-09-26

Is是一个完全骨干.js菜鸟的问题。我正在研究ToDo Backbone.js示例,试图构建一个相当简单的单个应用程序界面。虽然待办事项项目更多的是关于用户输入,但此应用程序更多的是根据用户选项(单击事件)过滤数据。

我对Backbone.js和Mongoose完全陌生,一直找不到一个很好的例子来说明我正在努力做的事情。我已经能够让我的 api 从 MongoDB 集合中提取数据并将其放入 Backbone.js 集合中,该集合在应用程序中呈现它。我一生都想不出如何做的是过滤该数据并重新呈现应用程序视图。我正在尝试按文档中的"类型"字段进行过滤。

这是我的脚本:

(我完全知道需要一些重大的重构,我只是在快速原型化一个概念。

$(function() {
    window.Job = Backbone.Model.extend({
        idAttribute: "_id",
        defaults: function() {
            return {
                attachments: false
            }
        }
    });
    window.JobsList = Backbone.Collection.extend({
        model: Job,
        url: '/api/jobs',
        leads: function() {
            return this.filter(function(job){ return job.get('type') == "Lead"; });
        }
    });
    window.Jobs = new JobsList;
    window.JobView = Backbone.View.extend({
        tagName: "div",
        className: "item",
        template: _.template($('#item-template').html()),
        initialize: function() {
            this.model.bind('change', this.render, this);
            this.model.bind('destroy', this.remove, this);
        },
        render: function() {
            $(this.el).html(this.template(this.model.toJSON()));
            this.setText();
            return this;
        },
        setText: function() {
            var month=new Array();
            month[0]="Jan";
            month[1]="Feb";
            month[2]="Mar";
            month[3]="Apr";
            month[4]="May";
            month[5]="Jun";
            month[6]="Jul";
            month[7]="Aug";
            month[8]="Sep";
            month[9]="Oct";
            month[10]="Nov";
            month[11]="Dec";
            var title = this.model.get('title');
            var description = this.model.get('description');
            var datemonth = this.model.get('datem');
            var dateday = this.model.get('dated');
            var jobtype = this.model.get('type');
            var jobstatus = this.model.get('status');
            var amount = this.model.get('amount');
            var paymentstatus = this.model.get('paymentstatus')
            var type = this.$('.status .jobtype');
            var status = this.$('.status .jobstatus');
            this.$('.title a').text(title);
            this.$('.description').text(description);
            this.$('.date .month').text(month[datemonth]);
            this.$('.date .day').text(dateday);
            type.text(jobtype);
            status.text(jobstatus);
            if(amount > 0)
                this.$('.paymentamount').text(amount)
            if(paymentstatus)
                this.$('.paymentstatus').text(paymentstatus)
            if(jobstatus === 'New') {
                status.addClass('new');
            } else if (jobstatus === 'Past Due') {
                status.addClass('pastdue')
            };
            if(jobtype === 'Lead') {
                type.addClass('lead');
            } else if (jobtype === '') {
                type.addClass('');
            };
        },
        remove: function() {
            $(this.el).remove();
        },
        clear: function() {
            this.model.destroy();
        }
    });
    window.AppView = Backbone.View.extend({
        el: $("#main"),
        events: {
            "click #leads .highlight" : "filterLeads"
        },
        initialize: function() {
            Jobs.bind('add', this.addOne, this);
            Jobs.bind('reset', this.addAll, this);
            Jobs.bind('all', this.render, this);
            Jobs.fetch();
        },
        addOne: function(job) {
            var view = new JobView({model: job});
            this.$("#activitystream").append(view.render().el);
        },
        addAll: function() {
            Jobs.each(this.addOne);
        },
        filterLeads: function() {
            // left here, this event fires but i need to figure out how to filter the activity list.
        }
    });
    window.App = new AppView;
});

您是否尝试过使用"潜在客户"过滤器的结果重置集合?

类似的东西

window.AppView = Backbone.View.extend({
    el: $("#main"),
    events: {
        "click #leads .highlight" : "filterLeads"
    },
    initialize: function() {
        Jobs.bind('add', this.addOne, this);
        Jobs.bind('reset', this.render, this); //render on reset
        Jobs.fetch(); //this triggers reset
    },
    addOne: function(job) {
        var view = new JobView({model: job});
        this.$("#activitystream").append(view.render().el);
    },
    //add a render function
    render: function() {
        this.$("#activitystream").empty(); //empty out anything thats in there
        Jobs.each(this.addOne);
    },
    filterLeads: function() {
        Jobs.reset(Jobs.leads()); //reset Jobs with only the leads
    }
});

此外,您的 AppView 没有"渲染"方法,但您可以在此处引用它:

Jobs.bind('all', this.render, this);