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 7888195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:46:18+00:00 2026-06-03T05:46:18+00:00

I have a backbone.js collection where I need to do a fulltextsearch on. The

  • 0

I have a backbone.js collection where I need to do a fulltextsearch on. The tools I have at hand are the following:

Backbone.js, underscore.js, jQuery

For those not familiar with backbone:

A backbones collection is just an object. Inside the collection there is an array with models. Each model has an array with attributes. I have to search each attribute for a string.

The code I am using for this is:

query = 'some user input';

query = $.trim(query);
query = query.replace(/ /gi, '|');

var pattern = new RegExp(query, "i");

// this.collection.forEach is the same as _.each
// only it get's the models from the collection
this.collection.forEach(function(model) {
    var check = true;
    _.each(model.attributes, function(attr){
        if(pattern.test(attr) && check){
            // Do something with the matched item
            check = false;
        }
    }, this);
}, this);

Maybe one of the tools I am using has a better way of dealing with this ?

  • 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-03T05:46:19+00:00Added an answer on June 3, 2026 at 5:46 am

    Backbone extends a lot of the underscore methods into the Collection class so you can get rid of some of that stuff. Really you probably want to impliment this on the collection itself as a method, then I would probably look at those keys using a good old fashioned for loop, especially if I wanted to break out of it.

    // in Backbone.Collection.extend
    search: function( query, callback ){
      var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
      var collection = this;
      collection.each(function(model) {
          for( k in model.attributes ){
            if( model.attributes.hasOwnProperty(k) && pattern.test(model.attributes[k]) ){ 
              callback.call( collection, model, k ); 
              break; // ends the for loop.
            }
          }
      });
    
    }
    
    // later
    collection.search('foo', function( model, attr ){
      console.log('found foo in '+model.cid+' attribute '+attr);
    });
    

    That said, this would only ever return the first match from the collection. You may prefer an implementation that returns an array of results as [model, attribute] pairs.

    // in Backbone.Collection.extend
    search: function( query, callback ){
      var matches = [];
      var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
      this.each(function(model) {
          for( k in model.attributes ){
            if( model.attributes.hasOwnProperty(k) && pattern.test(model.attributes[k]) ){ 
              matches.push([model, k]);
            }
          }
      });
      callback.call( this, matches );
    }
    
    // later
    collection.search('foo', function( matches ){
      _.each(matches, function(match){
        console.log('found foo in '+match[0].cid+' attribute '+match[1]);
      });
    });
    

    Or, if you wanted an array of models which matched but don’t care which attribute matched you can use filter

    // in Backbone.Collection.extend
    search: function( query, callback ){
      var pattern = new RegExp( $.trim( query ).replace( / /gi, '|' ), "i");
      callback.call( this, this.filter(function( model ){ 
        for( k in model.attributes ){ 
          if( model.attributes.hasOwnProperty(k) && pattern.test(k) ) 
            return true;
        }
      }));
    }
    
    // later
    collection.search('foo', function( matches ){
      _.each(matches, function(match){
        console.log('found foo in '+match[0].cid+' somewhere');
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have a Backbone Collection and I need to pass some parameters to the
I have Backbone.js collection that holds (for example) 30 items. I want to pass
I have a collection of flash cards that are tied to a Backbone collection.
I have a Backbone View that has a Collection as its Model. If the
Say I have the following Backbone Router: class App.Routers.ThingsRouter extends Backbone.Router initialize: -> new
I use Backbone.js and jQuery 1.7 in my application and I have some problems
As in the backbone-todolist example,I have a collection of elements. I made 2 views,
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
I have very simple backbone model and collection. I have a corresponding backbone.marionette.CollectionView and

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.