masylum / backbone.subset Goto Github PK
View Code? Open in Web Editor NEWProvides a collection-like constructor that allows you create a collection that is subset from a parent one
Provides a collection-like constructor that allows you create a collection that is subset from a parent one
If you create a new instance of a Backbone.Subset, it does not act as an instance of a Backbone.Collection:
!(mySubset instanceof Backbone.Collection) === true
This is a problem because it means you can't always substitute a Subset for a Collection. Knockback, for instance, can't make a collectionObservable around a Subset because it does an instanceof safety check.
As seen here: https://github.com/masylum/Backbone.Subset/blob/master/backbone.subset.js#L23-26
Backbone.Subset = function Subset(models, options) {
options = options || {};
this.model = this.parent().model;
The trouble with this is it effectively turns instances of Backbone.Subset into singletons, you have to define:
SomeSubset.prototype.parent = function() { /*....*/ }
In such a way that it is very aware of the program implemented around it.
The data inside the instances of a class ought to be passed into the constructor, not the implementation of the class itself.
In the reduce function of Subset.recalculate, short-circuit evaluation is causing the right side of:
return changed || self._updateModelMembership(model, {silent: true})
to never be eval'd once changed becomes true. This causes sieve to not be called on each element.
Most likely want:
return self._updateModelMembership(model, {silent: true}) || changed;
It may be a documentation issue, or it may be a bug, I can't tell.
Line 59 results in an error because this.models
(usually set in the Backbone.Collection constructor) hasn't been set yet. I'm not 100% sure what solution to suggest (thus why it's not a pull request) since I'm not really sure if it's a bug or documentation issue. Either way it's worth fixing because this is a very handy, well written tool.
Steps to Reproduce:
coll = new Backbone.Collection([], {})
new Backbone.Subset([], {parent: coll, sieve: function(m) { return true; }})
This happens whether or not you pass in the undocumented no_reset
option. If no_reset
is true the error fires at line 152
It appears this repo has gone dormant. I've been using this plugin for awhile, and fixed a couple bugs.
Since _byCid was deprecated, the _updateModelMembership function broke. This has been fixed.
When extending from a parent collection, the subset was not inheriting any extra properties that may have been set on it. This has also bee fixed.
Fork is here: https://github.com/sarink/Backbone.Subset - I will try to keep updating Pau's code as necessary because I still like this little subset library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.