gpt4 book ai didi

javascript - 过滤后如何使用原来的Backbone集合?

转载 作者:行者123 更新时间:2023-11-30 12:57:27 25 4
gpt4 key购买 nike

我对 Backbone 比较陌生,虽然我知道如何使用它的一般概念,但我的学习速度很快,我可能遗漏了一些关键元素。

所以我有一个集合,其中包含一个名为“类型”的属性,可以是文章、书籍、视频、类(class)。我有 View 渲染和所有内容,但我需要能够在单击链接时过滤集合。

我的问题是 - 当我点击另一种类型时,如何让它过滤掉集合并仍然能够重新过滤原始集合?

这是我的代码的要点,为了便于阅读,我对其进行了简化:

var TagsView = Backbone.View.extend({
initialize: function(query) {
this.collection = new TagsCollection([], {query: self.apiQuery} );
this.collection.on('sync', function() {
self.render();
});
this.collection.on('reset', this.render, this);
},
render: function() {
//renders the template just fine
},
filter: function() {
//filtered does work correctly the first time I click on it but not the second.
var filtered = this.collection.where({'type':filter});
this.collection.reset(filtered);
}
});

更新:我设法让它工作了。我最终触发了一个过滤事件。

var TagsCollection = Backbone.Collection.extend({
initialize: function(model, options) {
this.query = options.query;
this.fetch();
},
url: function() {
return '/api/assets?tag=' + this.query;
},
filterBy: function(filter) {
filtered = this.filter(function(asset) {
return asset.get('type') == filter;
});
this.trigger('filter');
return new TagsCollection(filtered, {query: this.query});
},
model: AssetModel
});

然后在我看来,我添加了一些东西来呈现我的新收藏。

var TagsView = Backbone.View.extend({
initialize: function(query) {
this.collection = new TagsCollection([], {query: self.apiQuery} );
this.collection.on('sync', function() {
self.render();
});
this.collection.on('filter sync', this.filterTemplate, this);
this.collection.on('reset', this.render, this);
},
render: function() {
//renders the template just fine
},
filterCollection: function(target) {
var filter = $(target).text().toLowerCase().slice(0,-1);
if (filter != 'al') {
var filtered = this.collection.filterBy(filter);
} else {
this.render();
}

},
filterTemplate: function() {
filterResults = new TagsCollection(filtered, {query: self.apiQuery});
console.log(filterResults);
$('.asset').remove();
filterResults.each(function(asset,index) {
dust.render('dust/academy-card', asset.toJSON(), function(error,output) {
self.$el.append(output);
});
});
},
});

最佳答案

它第二次不起作用的原因是因为您在调用 reset 时删除了与过滤器不匹配的模型。这是 reset 函数的正常行为。

不要使用 View 的主集合进行渲染,而是尝试使用第二个集合来进行渲染,它代表原始基础集合的过滤数据。所以你的观点可能看起来像:

var TagsView = Backbone.View.extend({

filter: null,

events: {
'click .filter-button': 'filter'
},

initialize: function (query) {
this.baseCollection = new TagsCollection([], {query: self.apiQuery} );
this.baseCollection.on('reset sync', this.filterCollection, this);
this.collection = new Backbone.Collection;
this.collection.on('reset', this.render, this);
},

render: function () {
var self = this,
data = this.collection.toJSON();

// This renders all models in the one template
dust.render('some-template', data, function (error, output) {
self.$el.append(output);
});
},

filter: function (e) {
// Grab filter from data attribute or however else you prefer
this.filter = $(e.currentTarget).attr('data-filter');
this.filterCollection();
},

filterCollection: function () {
var filtered;

if (this.filter) {
filtered = this.baseCollection.where({'type': this.filter});
} else {
filtered = this.baseCollection.models;
}

this.collection.reset(filtered);
}

});

要删除任何过滤器,请将类为 filter-button 的按钮设置为空 data-filter 属性。 collection 将随 baseCollection 的所有模型一起重置

关于javascript - 过滤后如何使用原来的Backbone集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18544041/

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