New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

backbone-sortable-collection

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-sortable-collection

Add sorting functionalites to Backbone Collections

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Backbone Sortable Collection

Build Status Code Climate Test Coverage

Sample Usage

var TurtleCollection = Backbone.SortableCollection.extend({

  // Comparators are named by the key in this object
  comparators: {
    initial: function (turtle) { return turtle.get('initial'); },

    // shorthand for function (food) { return turtle.get('food'); },
    food: 'getter',

    age: 'getter',

    weird: function (turtleA, turtleB) {
      if (turtleA.get('initial') === 'M') {
        return -1;
      } else if (turtleB.get('initial') === 'M') {
        return 1;
      } else {
        return 0;
      }
    }
  },

  // Sorts can be used to string comparators together for multi-sort
  // Useful for specifying fallback comparators and default behavior
  sorts: {
    // Comparators can be called by name, prefixed with '!' if the order should be descending
    weird: ['!weird', 'initial'],

    // Also sorts can be called as a string if there is only one comparator
    oldest: '!age'

    // By default, sort only uses the comparator, so following line is redundant
    // initial: ['initial']
  },

  defaultSort: 'initial'
});

var turtles = new TurtleCollection([
  { initial: 'L', food: 'rice', age: 15.8 },
  { initial: 'R', food: 'cereal', age: 15.7 },
  { initial: 'D', food: 'pizza', age: 15.5 },
  { initial: 'M', food: 'pizza', age: 15.3 }
]);

// Default sort is active
console.log(turtles.pluck('initial')); // ['D', 'L', 'M', 'R']

// Single sort
turtles.changeSort('food');
console.log(turtles.pluck('initial')); // ['R', 'D', 'M', 'L']

// Bi-directional multi-sort
turtles.changeSort('weird');
// turtles.changeSort(['!weird', 'initial']); would achieve same result
console.log(turtles.pluck('initial')); // ['D', 'L', 'R', 'M']

// Fires sort event
turtes.on('sort', function () { console.log('Sorting!'); });
turtles.changeSort('initial'); // Sorting!

// Reverse sort (also fires sort event)
turtles.reverseSort(); // Sorting!
console.log(turtles.pluck('initial')); // ['M', 'R', 'L', 'D']

Installation

# Node
npm install backbone-sortable-collection --save

# Bower
bower install backbone-sortable-collection

Keywords

FAQs

Package last updated on 26 Feb 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc