Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:27:50+00:00 2026-06-09T08:27:50+00:00

The current method I’m using is to filter a collection, which returns an array,

  • 0

The current method I’m using is to filter a collection, which returns an array, and use

collection.reset(array)

to re-populate it. However, this modifies the original collection, so I added an array called “originalCollectionArray” which keeps track of the initial array state of the collection. When no filtering is active I simply use

collection.reset(originalCollectionArray)

But then, I need to keep track of adding and removing models from the real collection, so I did this:

// inside collection
initialize: function(params){
    this.originalCollectionArray = params;
    this.on('add', this.addInOriginal, this);
    this.on('remove', this.removeInOriginal, this);
},
addInOriginal: function(model){
    this.originalCollectionArray.push(model.attributes);
},
removeInOriginal: function(model){
    this.originalTasks = _(this.originalTasks).reject(function(val){
        return val.id == model.get('id');
    });
},
filterBy: function(params){
    this.reset(this.originalCollectionArray, {silent: true});
    var filteredColl = this.filter(function(item){
        // filter code...
    });
    this.reset(filteredColl);
}

This is quickly becoming cumbersome as I try to implement other tricks related to the manipulation of the collection, such as sorting. And frankly, my code looks a bit hacky. Is there an elegant way of doing this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:27:51+00:00Added an answer on June 9, 2026 at 8:27 am

    You could create a collection as a property of the main collection reflecting the state of the filters:

    var C = Backbone.Collection.extend({
        initialize: function (models) {
            this.filtered = new Backbone.Collection(models);
            this.on('add', this.refilter);
            this.on('remove', this.refilter);
        },
    
        filterBy: function (params){
            var filteredColl = this.filter(function(item){
              // ...
            });
    
            this.filtered.params = params;
            this.filtered.reset(filteredColl);
        },
    
        refilter: function() {
            this.filterBy(this.filtered.params);
        }
    });
    

    The parent collection keeps its models whatever filters you applied, and you bind to the filtered collection to know when a change has occurred. Binding internally on the add and remove events lets you reapply the filter. See
    http://jsfiddle.net/dQr7X/ for a demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm displaying currency using the current method String.Format({0:C}, item.DonationAmount) Which outputs like $10.00 We
My current method is this: SELECT TOP 1 ID FROM DATAENTRY ORDER BY ID
At my company, our current method of updating the database is to connect using
It is possible to use the getRowData method to retrieve the current of a
I know how to get the current method ( MethodBase.GetCurrentMethod() ). However, the DeclaringType
In my current method, I use order by date in sql query, then set
I'm trying to output a PDF using server side javascript (ASP). The current method
The current method I use simply increments an index value on afterChange event, but
I have an array of unknown (to the current method) class objects. I do
Is there a way to check which interceptors are present for the current method?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.