gpt4 book ai didi

javascript - 如何过滤 Backbone.js Collection 和 Rerender App View?

转载 作者:可可西里 更新时间:2023-11-01 09:12:35 26 4
gpt4 key购买 nike

这是一个完整的 Backbone.js 菜鸟问题。我正在尝试构建一个相当简单的单一应用程序界面的 ToDo Backbone.js 示例。 todo 项目更多的是关于用户输入,而这个应用程序更多的是关于根据用户选项(点击事件)过滤数据。

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

这是我的脚本:

(我完全知道需要进行一些重大重构,我只是在快速制作概念原型(prototype)。)

$(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;

});

最佳答案

你试过了吗resetting包含“线索”过滤器结果的集合?

有点像

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);

关于javascript - 如何过滤 Backbone.js Collection 和 Rerender App View?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10955462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com