Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
backbone-collection-search
Advanced tools
A keyword search plugin for Backbone.Collections
This plugin adds a "search" method to allow keyword search through the model's attributes in the collection. This is a great plugin for implementation of typeahead widgets.
collection.search( [query string] );
collection.on('search', function( results ) { ... });
Search requires a query string and accepts an optional attributes parameter. All the attributes will be searched from each model unless you provide an array of attributes. When the results are ready, a trigger "search" will fire and a new collection of the models are returned.
var Book = Backbone.Model;
var books = new Backbone.Collection();
books.add(new Book({ author: 'J. K. Rowling', related: 'Stephanie Meyer books', title: 'Harry Potter #1' }));
books.add(new Book({ author: 'P&B Pollack', related: 'Stephanie Meyer books', title: 'Who Is J. K. Rowling?' }));
books.add(new Book({ author: 'Stephanie Meyer', related: 'J. K. Rowling books', title: 'Twilight'}));
books.search('rowling', ['author','title']);
books.on('search', function( results ){ alert( results.pluck('title') ) });
// Will result: "Harry Potter #1,Who Is J. K. Rowling?"
books.search('rowling');
books.on('search', function( results ){ alert( results.pluck('title') ) });
// Will result: "Harry Potter #1,Who Is J. K. Rowling?,Twilight"
matcher
is used to test the query against the attribute value. In the given default method, the search is doing a simple "indexOf" string search. It returns true
if it finds a match. This can be customized to a more robust matcher if desired (i.e. RegExp). In theory you may use different object types, but the provided method is dealing with strings.
This will return the most recent queried results. The resulted collection also has a method getSearchQuery()
to get the search query used to get that result.
books.on('search', function(){
var results = this.getSearchResults();
alert( results.pluck('title') )
// Will result: "Harry Potter #1,Who Is J. K. Rowling?"
alert( results.getSearchQuery() )
// Will result: "rowling"
});
This will return the most recent search query. It will pull directly from getSearchResults()
Like the built in underscore methods, it returns the collection. Note: this won't work with the ajax version.
var stephanieBooks = books.search('stephanie', ['author']);
alert(stephanieBooks.pluck('title'))
// Will result: "Twilight"
If a front-end solution doesn't suffice, there is also an ajax support using the fetch
function and ability to add parameters. This is a starter piece to making that server-side search function. The mark up on the front-end will be exactly the same, and it will similarly fire 'search' when the call is finished. The appended parameters are keyword
and attributes[]
GET http://somelibrary.com/api/books?keyword=rowling&attributes[]=author&attributes[]=title
The plugin is non-destructive to all the existing behaviors.
require.config({
paths: {
'underscore': 'assets/js/underscore',
'backbone': 'assets/js/backbone',
'backbone-collection-search': 'assets/js/backbone-collection-search',
'backbone-collection-search-ajax' : 'asset/js/backbone-collection-search-ajax'
},
shim: {
'backbone': {
deps: ['underscore'],
exports: 'Backbone'
},
'backbone-collection-search': {
deps: ['underscore', 'backbone'],
exports: 'Backbone'
},
'backbone-collection-search-ajax': {
deps: ['underscore', 'backbone'],
exports: 'Backbone'
}
}
});
define(['backbone-collection-search'], function( Backbone ) { ... });
OR
define(['backbone-collection-search-ajax'], function( Backbone ) { ... });
<script src="assets/js/underscore.js"></script>
<script src="assets/js/backbone.js"></script>
<script src="assets/js/backbone-collection-search.js"></script>
OR
<script src="assets/js/backbone-collection-search-ajax.js"></script>
Streamlined the way to retrieve the recent query's keyword and attribute
getSearchQuery()
now returns an object from the defined "searching" object literal in the new collectionCode needed some love and refactoring so I had to make the changes
delay
because I thought it was unnessary. Any delay (like for a typeahead widget) should be done in the View.FAQs
A keyword search plugin for Backbone.Collections
The npm package backbone-collection-search receives a total of 0 weekly downloads. As such, backbone-collection-search popularity was classified as not popular.
We found that backbone-collection-search demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.