Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 5948cc034ffcf8893e9fb40c1aca71bf15ea4478 @masylum committed Nov 25, 2011
Showing with 28,920 additions and 0 deletions.
  1. +6 −0 Makefile
  2. +68 −0 Readme.md
  3. +187 −0 backbone.subset.js
  4. +1 −0 node_modules/.bin/mocha
  5. +1 −0 node_modules/.bin/mocha-debug
  6. +1 −0 node_modules/backbone/.gitignore
  7. +22 −0 node_modules/backbone/LICENSE
  8. +25 −0 node_modules/backbone/README
  9. +42 −0 node_modules/backbone/Rakefile
  10. +1,158 −0 node_modules/backbone/backbone.js
  11. +56 −0 node_modules/backbone/docs/backbone-localstorage.html
  12. +728 −0 node_modules/backbone/docs/backbone.html
  13. +186 −0 node_modules/backbone/docs/docco.css
  14. BIN node_modules/backbone/docs/images/arrows.png
  15. BIN node_modules/backbone/docs/images/backbone-mobile.png
  16. BIN node_modules/backbone/docs/images/backbone.png
  17. BIN node_modules/backbone/docs/images/background.png
  18. BIN node_modules/backbone/docs/images/baroque.jpg
  19. BIN node_modules/backbone/docs/images/basecamp-mobile.png
  20. BIN node_modules/backbone/docs/images/bittorrent.jpg
  21. BIN node_modules/backbone/docs/images/blossom.png
  22. BIN node_modules/backbone/docs/images/chop.png
  23. BIN node_modules/backbone/docs/images/cloudapp.png
  24. BIN node_modules/backbone/docs/images/dc-workspace.png
  25. BIN node_modules/backbone/docs/images/decide.png
  26. BIN node_modules/backbone/docs/images/flow.png
  27. BIN node_modules/backbone/docs/images/fluxiom.png
  28. BIN node_modules/backbone/docs/images/groupon.png
  29. BIN node_modules/backbone/docs/images/hotel-tonight.png
  30. BIN node_modules/backbone/docs/images/instagreat.png
  31. BIN node_modules/backbone/docs/images/menagerievet.png
  32. BIN node_modules/backbone/docs/images/pandora.png
  33. BIN node_modules/backbone/docs/images/quietwrite.png
  34. BIN node_modules/backbone/docs/images/quoteroller.png
  35. BIN node_modules/backbone/docs/images/seatgeek.png
  36. BIN node_modules/backbone/docs/images/soundcloud.png
  37. BIN node_modules/backbone/docs/images/substance.png
  38. BIN node_modules/backbone/docs/images/test-kitchen.png
  39. BIN node_modules/backbone/docs/images/tilemill.png
  40. BIN node_modules/backbone/docs/images/todos.png
  41. BIN node_modules/backbone/docs/images/trajectory.png
  42. BIN node_modules/backbone/docs/images/trapit.png
  43. BIN node_modules/backbone/docs/images/tzigla.png
  44. +44 −0 node_modules/backbone/docs/jsl.conf
  45. +122 −0 node_modules/backbone/docs/todos.html
  46. +84 −0 node_modules/backbone/examples/backbone-localstorage.js
  47. BIN node_modules/backbone/examples/todos/destroy.png
  48. +87 −0 node_modules/backbone/examples/todos/index.html
  49. +311 −0 node_modules/backbone/examples/todos/todos.css
  50. +249 −0 node_modules/backbone/examples/todos/todos.js
  51. +2,863 −0 node_modules/backbone/index.html
  52. +1 −0 node_modules/backbone/index.js
  53. +14 −0 node_modules/backbone/package.json
  54. +345 −0 node_modules/backbone/test/collection.js
  55. +86 −0 node_modules/backbone/test/events.js
  56. +43 −0 node_modules/backbone/test/model.coffee
  57. +426 −0 node_modules/backbone/test/model.js
  58. +12 −0 node_modules/backbone/test/noconflict.js
  59. +116 −0 node_modules/backbone/test/router.js
  60. +45 −0 node_modules/backbone/test/speed.js
  61. +133 −0 node_modules/backbone/test/sync.js
  62. +30 −0 node_modules/backbone/test/test-zepto.html
  63. +31 −0 node_modules/backbone/test/test.html
  64. +8,176 −0 node_modules/backbone/test/vendor/jquery-1.5.js
  65. +649 −0 node_modules/backbone/test/vendor/jslitmus.js
  66. +481 −0 node_modules/backbone/test/vendor/json2.js
  67. +196 −0 node_modules/backbone/test/vendor/qunit.css
  68. +1,364 −0 node_modules/backbone/test/vendor/qunit.js
  69. +807 −0 node_modules/backbone/test/vendor/underscore-1.1.6.js
  70. +692 −0 node_modules/backbone/test/vendor/zepto-0.6.js
  71. +137 −0 node_modules/backbone/test/view.js
  72. +4 −0 node_modules/mocha/.gitignore
  73. +4 −0 node_modules/mocha/.npmignore
  74. +4 −0 node_modules/mocha/.travis.yml
  75. +78 −0 node_modules/mocha/History.md
  76. +79 −0 node_modules/mocha/Makefile
  77. +31 −0 node_modules/mocha/Readme.md
  78. +205 −0 node_modules/mocha/bin/mocha
  79. +3 −0 node_modules/mocha/bin/mocha-debug
  80. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/bdd - after each.tmSnippet
  81. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/bdd - after.tmSnippet
  82. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/bdd - before each.tmSnippet
  83. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/bdd - before.tmSnippet
  84. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/bdd - it.tmSnippet
  85. +16 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/Snippets/untitled.tmSnippet
  86. +19 −0 node_modules/mocha/editors/JavaScript mocha.tmbundle/info.plist
  87. +2 −0 node_modules/mocha/index.js
  88. +53 −0 node_modules/mocha/lib/browser/events.js
  89. +8 −0 node_modules/mocha/lib/browser/tty.js
  90. +86 −0 node_modules/mocha/lib/interfaces/bdd.js
  91. +60 −0 node_modules/mocha/lib/interfaces/exports.js
  92. +4 −0 node_modules/mocha/lib/interfaces/index.js
  93. +70 −0 node_modules/mocha/lib/interfaces/tdd.js
  94. +18 −0 node_modules/mocha/lib/mocha.js
  95. +226 −0 node_modules/mocha/lib/reporters/base.js
  96. +78 −0 node_modules/mocha/lib/reporters/doc.js
  97. +62 −0 node_modules/mocha/lib/reporters/dot.js
  98. +126 −0 node_modules/mocha/lib/reporters/html.js
  99. +11 −0 node_modules/mocha/lib/reporters/index.js
  100. +63 −0 node_modules/mocha/lib/reporters/json-stream.js
  101. +72 −0 node_modules/mocha/lib/reporters/json.js
  102. +97 −0 node_modules/mocha/lib/reporters/landing.js
  103. +61 −0 node_modules/mocha/lib/reporters/list.js
  104. +84 −0 node_modules/mocha/lib/reporters/progress.js
  105. +83 −0 node_modules/mocha/lib/reporters/spec.js
  106. +51 −0 node_modules/mocha/lib/reporters/tap.js
  107. +394 −0 node_modules/mocha/lib/runner.js
  108. +196 −0 node_modules/mocha/lib/suite.js
  109. +83 −0 node_modules/mocha/lib/test.js
  110. +16 −0 node_modules/mocha/lib/utils.js
  111. +91 −0 node_modules/mocha/mocha.css
  112. +2,173 −0 node_modules/mocha/mocha.js
  113. +23 −0 node_modules/mocha/package.json
  114. +150 −0 node_modules/mocha/support/compile.js
  115. +39 −0 node_modules/mocha/support/tail.js
  116. +35 −0 node_modules/mocha/test/browser/array.js
  117. +22 −0 node_modules/mocha/test/browser/index.html
  118. +96 −0 node_modules/mocha/test/browser/style.css
  119. +20 −0 node_modules/mocha/test/duration.js
  120. +18 −0 node_modules/mocha/test/fs.js
  121. +19 −0 node_modules/mocha/test/globals.js
  122. +107 −0 node_modules/mocha/test/hook.async.js
  123. +75 −0 node_modules/mocha/test/hook.sync.js
  124. +66 −0 node_modules/mocha/test/hook.sync.nested.js
  125. +17 −0 node_modules/mocha/test/http.js
  126. +82 −0 node_modules/mocha/test/http.meta.2.js
  127. +52 −0 node_modules/mocha/test/http.meta.js
  128. +25 −0 node_modules/mocha/test/interfaces/bdd.js
  129. +44 −0 node_modules/mocha/test/interfaces/exports.js
  130. +15 −0 node_modules/mocha/test/interfaces/tdd.js
  131. +34 −0 node_modules/mocha/test/merge.js
  132. +58 −0 node_modules/mocha/test/misc/cascade.js
  133. +22 −0 node_modules/mocha/test/misc/grep.js
  134. +10 −0 node_modules/mocha/test/misc/nontty.js
  135. +4 −0 node_modules/mocha/test/mocha.opts
  136. +10 −0 node_modules/mocha/test/multiple.done.js
  137. +4 −0 node_modules/mocha/test/pending.js
  138. +6 −0 node_modules/mocha/test/test.coffee
  139. +14 −0 node_modules/mocha/test/timeout.js
  140. +10 −0 node_modules/mocha/test/uncaught.js
  141. +3 −0 node_modules/underscore/.npmignore
  142. +22 −0 node_modules/underscore/LICENSE
  143. +19 −0 node_modules/underscore/README
  144. +1,759 −0 node_modules/underscore/index.html
  145. +1 −0 node_modules/underscore/index.js
  146. +12 −0 node_modules/underscore/package.json
  147. +30 −0 node_modules/underscore/underscore-min.js
  148. +977 −0 node_modules/underscore/underscore.js
  149. +9 −0 package.json
  150. +195 −0 test/test.js
@@ -0,0 +1,6 @@
+NODE = node
+
+test:
+ ./node_modules/.bin/mocha --reporter spec
+
+.PHONY: test
@@ -0,0 +1,68 @@
+# 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.
+ * You must implement a `sieve` function that will be used to filter the parent collection.
+ * You can pass the option `{noparent: true}` on doing an `add` or a `remove` if you don't want this action to bubble to the parent collection.
+
+``` javascript
+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(task.length, 2);
+assert.equal(archivedTasks.length, 1);
+```
+
+## Tests
+
+You must have node installed in order to run the tests.
+
+```
+npm install
+make
+```
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010-2011 Pau Ramon <masylum@gmail.com>
+
+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.
@@ -0,0 +1,187 @@
+/**
+ * @class Backbone.Subset
+ * @name Backbone Subset collections
+ * @desc Implements a collection that is a subset other Backbone Collections
+*/
+(function () {
+
+ var root = this
+ , Subset = {}
+ , _ = root._;
+
+ if (!_ && (typeof require !== 'undefined')) {
+ _ = require('underscore')._;
+ }
+
+ /**
+ * Subset constructor
+ *
+ * @param {String|Object} type
+ * @param {Number} id
+ * @return {Object}
+ */
+ Backbone.Subset = function Subset(models, options) {
+ options = options || {};
+
+ this.model = this.parent().model;
+ this.comparator = options.comparator || this.parent().comparator;
+
+ _.bindAll(this, '_onModelEvent', '_unbindModelEvents', '_proxyEvents');
+
+ this.parent().bind('all', this._proxyEvents);
+
+ this._reset();
+ this.reset(models || this.parent().models, {silent: true});
+ this.initialize.apply(this, arguments);
+ };
+
+ /**
+ * Resets the parent collection
+ *
+ * @param {Object} models
+ * @param {Object} options
+ * @return {Object} collection
+ */
+ Subset.reset = function (models, options) {
+ models = models || [];
+ models = _.isArray(models) ? models : [models];
+ options = options || {};
+
+ // delete parent reseted models
+ this.each(function (model) {
+ this.parent()._remove(model, _.extend(options, {noproxy: true}));
+ }, this);
+
+ // insert parent reseted models
+ _.each(models, function (model) {
+ this.parent()._add(model, _.extend(options, {noproxy: true}));
+ }, this);
+
+ return this._resetSubset(models, options);
+ };
+
+ /**
+ * Resets the subset collection
+ *
+ * @param {Object} models
+ * @param {Object} options
+ * @return {Object} collection
+ */
+ Subset._resetSubset = function (models, options) {
+ models = models || [];
+ models = _.isArray(models) ? models : [models];
+ options = options || {};
+ this.each(this._unbindModelEvents);
+ this._reset();
+
+ _.each(models, function (model) {
+ this._addToSubset(model, {silent: true});
+ }, this);
+
+ if (!options.silent) {
+ this.trigger('reset', this, options);
+ }
+
+ return this;
+ };
+
+ /**
+ * Adds a model into the parent collection
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._add = function (model, options) {
+ return this.parent()._add(model, options);
+ };
+
+ /**
+ * Adds a model into the subset collection
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._addToSubset = function (model, options) {
+ return Backbone.Collection.prototype._add.call(this, model, options);
+ }
+
+ /**
+ * Remove a model from the subset collection
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._remove = function (model, options) {
+ return this.parent()._remove(model, options);
+ };
+
+ /**
+ * Removes a model from the subset collection
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._removeFromSubset = function (model, options) {
+ return Backbone.Collection.prototype._remove.call(this, model, options);
+ }
+
+ /**
+ * Prepare a model to be added to a collection
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._prepareModel = function (model, options) {
+ if (!(model instanceof Backbone.Model)) {
+ var attrs = model;
+ model = new this.model(attrs, {collection: this.parent()});
+
+ if (model.validate && !model._performValidation(model.attributes, options)) {
+ model = false;
+ }
+ } else if (!model.collection) {
+ model.collection = this.parent();
+ }
+ model = this.sieve(model) ? model : false;
+ return model;
+ };
+
+ /**
+ * Proxies an event happening into the parent collection to the Subset
+ *
+ * @param {Object} model
+ * @param {Object} options
+ * @return {Object} model
+ */
+ Subset._proxyEvents = function (ev, model, collection, options) {
+ if (ev === 'add' && collection !== this && this.sieve(model) && !options.noproxy) {
+ this._addToSubset(model, options);
+ }
+
+ if (ev === 'remove' && collection !== this && this.sieve(model) && !options.noproxy) {
+ this._removeFromSubset(model, options);
+ }
+
+ // model == collection
+ if (ev === 'reset' && model !== this && model.any(this.sieve)) {
+ this._resetSubset(model.models, collection);
+ }
+ };
+
+ /**
+ * Unbinds the _onModelEvent listener
+ *
+ * @param {Object} model
+ */
+ Subset._unbindModelEvents = function (model) {
+ model.unbind('all', this._onModelEvent);
+ };
+
+ _.extend(Backbone.Subset.prototype, Backbone.Collection.prototype, Subset);
+ Backbone.Subset.extend = Backbone.Collection.extend;
+}).call(this);
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 5948cc0

Please sign in to comment.