Giter Site home page Giter Site logo

latentflip / backbone.subset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from masylum/backbone.subset

2.0 2.0 0.0 4.1 MB

Provides a collection-like constructor that allows you create a collection that is subset from a parent one

JavaScript 100.00%

backbone.subset's Introduction

Backbone.Subset

A subset collection that contains pointers to models from a parent collection.

Use case?

Having a collection that represents only a subset of your models.

For instance having a Archived tasks, you want to be sure that adding, deleting or changing a Task will update that subset collection according to a given sieve

How does it work?

The API is almost the same as Backbone.Collection.

  • You must implement a parent function that returns the collection the subset belongs or pass a parent option.
  • You must implement a sieve function that will be used to filter the parent collection.
  • You can pass the option {noproxy: true} if you don't want the event to bubble from the parent to the subset (not recommended since it can leave your collections in a weird state).
Models.Task = Backbone.Model.extend({
  initialize: function () {
    this.collection = tasks;
  }
, isArchived: function () {
    return this.get('archived');
  }
});

Collections.Tasks = Backbone.Collection.extend({model: Models.Task});
Collections.ArchivedTasks = Backbone.Subset.extend({
  parent: function () {
    return tasks;
  }
, sieve: function (task) {
    return task.isArchived();
  }
});

tasks = new Collections.Tasks();
archivedTasks = new Collections.ArchivedTasks();

tasks.reset([{archived: true}, {archived: false}]);

assert.equal(tasks.length, 2);
assert.equal(archivedTasks.length, 1);

Live Updating of subsets

Subsets can be optionally updated live. That is, if a models attributes change such that affect it's membership of a subset, that update will happen automatically.

By default live updating is disabled. Continuing the above example:

tasks.reset([{archived: true}, {archived: false}]);
archivedTasks.liveupdate_keys = 'all'

assert.equal(tasks.length, 2);
assert.equal(archivedTasks.length, 1);

tasks.first().set({archived: false});
assert.equal(tasks.length, 2);
assert.equal(archivedTasks.length, 0);

Controlling live updating

Live updating is controlled by a subset's liveupdate\_keys attribute.

  • To prevent live updating of a subset, set it's liveupdate\_keys attribute to be 'none' (this is the default).
  • To enable live updating when any model key changes, set liveupdate\_keys = 'all'.
  • To limit which model keys trigger a live update, set liveupdate\_keys to be an array of attributes: liveupdate\_keys = ['archived'].

Tests

You must have node installed in order to run the tests.

npm install
make

Benchmarks

You must have node installed in order to run the benchmarks.

npm install
make benchmark

License

(The MIT License)

Copyright (c) 2010-2012 Pau Ramon [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

backbone.subset's People

Contributors

latentflip avatar masylum avatar saimonmoore avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.