backbone-filtered-collection 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ beforeEach(function() {
2
+ this.addMatchers({
3
+ toBePlaying: function(expectedSong) {
4
+ var player = this.actual;
5
+ return player.currentlyPlayingSong === expectedSong &&
6
+ player.isPlaying;
7
+ }
8
+ });
9
+ });
@@ -0,0 +1,76 @@
1
+ # src_files
2
+ #
3
+ # Return an array of filepaths relative to src_dir to include before jasmine specs.
4
+ # Default: []
5
+ #
6
+ # EXAMPLE:
7
+ #
8
+ # src_files:
9
+ # - lib/source1.js
10
+ # - lib/source2.js
11
+ # - dist/**/*.js
12
+ #
13
+ src_files:
14
+ - '__spec__/dependencies/underscore.js'
15
+ - '__spec__/dependencies/backbone.js'
16
+ - 'vendor/assets/javascripts/**/*.js'
17
+
18
+ # stylesheets
19
+ #
20
+ # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
21
+ # Default: []
22
+ #
23
+ # EXAMPLE:
24
+ #
25
+ # stylesheets:
26
+ # - css/style.css
27
+ # - stylesheets/*.css
28
+ #
29
+ stylesheets:
30
+
31
+ # helpers
32
+ #
33
+ # Return an array of filepaths relative to spec_dir to include before jasmine specs.
34
+ # Default: ["helpers/**/*.js"]
35
+ #
36
+ # EXAMPLE:
37
+ #
38
+ # helpers:
39
+ # - helpers/**/*.js
40
+ #
41
+ helpers:
42
+ - 'helpers/**/*.js'
43
+ # spec_files
44
+ #
45
+ # Return an array of filepaths relative to spec_dir to include.
46
+ # Default: ["**/*[sS]pec.js"]
47
+ #
48
+ # EXAMPLE:
49
+ #
50
+ # spec_files:
51
+ # - **/*[sS]pec.js
52
+ #
53
+ spec_files:
54
+ - '**/*[sS]pec.js'
55
+
56
+ # src_dir
57
+ #
58
+ # Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
59
+ # Default: project root
60
+ #
61
+ # EXAMPLE:
62
+ #
63
+ # src_dir: public
64
+ #
65
+ src_dir:
66
+
67
+ # spec_dir
68
+ #
69
+ # Spec directory path. Your spec_files must be returned relative to this path.
70
+ # Default: spec/javascripts
71
+ #
72
+ # EXAMPLE:
73
+ #
74
+ # spec_dir: spec/javascripts
75
+ #
76
+ spec_dir:
@@ -0,0 +1,211 @@
1
+ /*
2
+ Copyright (C) 2012 Dmitriy Likhten
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+ /* version 1.0.0 */
23
+ (function(_, Backbone) {
24
+ var defaultFilter = function() {return true;};
25
+ /**
26
+ * This represents a filtered collection. You can either pass a filter or
27
+ * invoke setFilter(filter) to give a filter. The filter is identical to
28
+ * that which is used for _.select(array, filter)
29
+ *
30
+ * false filter indicates no filtering.
31
+ *
32
+ * do not modify this collection directly via #add/#remove, modify the
33
+ * underlying origModel.
34
+ *
35
+ * Events:
36
+ * add - something was added (via filter or trickling from underlying collection)
37
+ * remove - something was removed (via filter or trickling from underlying collection)
38
+ * reset - the whole thing was reset
39
+ * sort - same as reset, but via sort
40
+ * filter-complete - filtering is complete -- very useful if you want to trigger a re-draw
41
+ * of the whole collection
42
+ */
43
+ Backbone.FilteredCollection = Backbone.Collection.extend({
44
+ collectionFilter: null
45
+ ,defaultFilter: defaultFilter
46
+
47
+ ,initialize: function(models, data) {
48
+ if (models) throw "models cannot be set directly, unfortunately first argument is the models.";
49
+ this.collection = data.collection;
50
+ this.setFilter(data.collectionFilter);
51
+
52
+ this.collection.on("add", this.addModel, this);
53
+ this.collection.on("remove", this.removeModel, this);
54
+ this.collection.on("reset", this.resetCollection, this);
55
+ this.collection.on("sort", this.resortCollection, this);
56
+ this.collection.on("change", this._modelChanged, this);
57
+ this.collection.on("filter-complete", this._filterComplete, this);
58
+ }
59
+
60
+ ,_reset: function(options) {
61
+ Backbone.Collection.prototype._reset.call(this, options);
62
+ this._mapping = [];
63
+ }
64
+
65
+ ,add: function() {
66
+ throw "Do not invoke directly";
67
+ }
68
+
69
+ ,remove: function() {
70
+ throw "Do not invoke directly";
71
+ }
72
+
73
+ ,reset: function() {
74
+ throw "Do not invoke directly";
75
+ }
76
+
77
+ ,_modelChanged: function(model, collection, options){
78
+ options || (options = {});
79
+
80
+ var ownIndexOfModel = this.indexOf(model);
81
+ if (this.collectionFilter(model)){
82
+ // Model passed filter
83
+ if (ownIndexOfModel < 0){
84
+ // Model not found, add it
85
+ var index = this.collection.indexOf(model);
86
+ this._forceAddModel(model, {index: index});
87
+ }
88
+ } else {
89
+ // Model did not pass filter
90
+ if (ownIndexOfModel > -1){
91
+ this._forceRemoveModel(model, {index: ownIndexOfModel});
92
+ }
93
+ }
94
+ if (! options.silent) {
95
+ this._filterComplete();
96
+ }
97
+ }
98
+
99
+ ,resortCollection: function() {
100
+ this._mapping = [];
101
+ this._reset();
102
+ this.setFilter(undefined, {silent: true});
103
+ this.trigger("sort", this);
104
+ }
105
+ ,resetCollection: function() {
106
+ this._mapping = [];
107
+ this._reset();
108
+ this.setFilter(undefined, {silent: true});
109
+ this.trigger("reset", this);
110
+ }
111
+
112
+ // this is to synchronize where the element exists in the original model
113
+ // to our _mappings array
114
+ ,renumberMappings: function() {
115
+ this._mapping = []
116
+ var collection = this.collection;
117
+ var mapping = this._mapping;
118
+
119
+ _(this.models).each(function(model) {
120
+ mapping.push(collection.indexOf(model));
121
+ });
122
+ }
123
+
124
+ ,removeModel: function(model, colleciton, options) {
125
+ var at = this._mapping.indexOf(options.index);
126
+ if (at > -1) {
127
+ this._forceRemoveModel(model, _.extend({index: at}, options));
128
+ }
129
+ this.renumberMappings();
130
+ }
131
+
132
+ // the options.index here is the index of the current model which we are removing
133
+ ,_forceRemoveModel: function(model, options) {
134
+ this._mapping.splice(options.index, 1);
135
+ Backbone.Collection.prototype.remove.call(this, model, {silent: options.silent});
136
+ if (! options.silent) {
137
+ this.trigger("remove", model, this, {index: options.index})
138
+ }
139
+ }
140
+
141
+ ,addModel: function(model, collection, options) {
142
+ if (this.collectionFilter(model)) {
143
+ this._forceAddModel(model, _.extend(options || {}, {index: (options && options.at) || collection.indexOf(model)}));
144
+ }
145
+ this.renumberMappings();
146
+ }
147
+
148
+ // the options.index here is the index of the original model which we are inserting
149
+ ,_forceAddModel: function(model, options) {
150
+ var desiredIndex = options.index;
151
+ // determine where to add, look at mapping and find first object with the index
152
+ // great than the one that we are given
153
+ var addToIndex = _.sortedIndex(this._mapping, desiredIndex, function(origIndex) { return origIndex; });
154
+
155
+ // add it there
156
+ Backbone.Collection.prototype.add.call(this, model, {at: addToIndex, silent: options.silent});
157
+ this._mapping.splice(addToIndex, 0, desiredIndex);
158
+ if (! options.silent) {
159
+ this.trigger("add", model, this, {index: addToIndex})
160
+ }
161
+ }
162
+
163
+ ,setFilter: function(newFilter, options) {
164
+ options || (options = {});
165
+ if (newFilter === false) { newFilter = this.defaultFilter } // false = clear out filter
166
+ this.collectionFilter = newFilter || this.collectionFilter || this.defaultFilter;
167
+
168
+ // this assumes that the original collection was unmodified
169
+ // without the use of add/remove/reset events. If it was, a
170
+ // reset event must be thrown, or this object's .resetCollection
171
+ // method must be invoked, or this will most likely fall out-of-sync
172
+
173
+ // why HashMap lookup when you can get it off the stack
174
+ var filter = this.collectionFilter;
175
+ var mapping = this._mapping;
176
+
177
+ // this is the option object to pass, it will be mutated on each
178
+ // iteration
179
+ var passthroughOption = _.extend({}, options);
180
+ this.collection.each(function(model, index) {
181
+ var foundIndex = mapping.indexOf(index);
182
+
183
+ if (filter(model, index)) {
184
+ // if already added, no touchy
185
+ if (foundIndex == -1) {
186
+ passthroughOption.index = index
187
+ this._forceAddModel(model, passthroughOption);
188
+ }
189
+ }
190
+ else {
191
+ if (foundIndex > -1) {
192
+ passthroughOption.index = foundIndex == -1 ? this.length : foundIndex;
193
+ this._forceRemoveModel(model, passthroughOption);
194
+ }
195
+ }
196
+ }, this);
197
+ if (! options.silent) {
198
+ this._filterComplete();
199
+ }
200
+ }
201
+
202
+ ,_onModelEvent: function(event, model, collection, options) {
203
+ // noop, this collection has no business dealing with events of the original model
204
+ // they will be handled by the original normal collection and bubble up to here
205
+ }
206
+
207
+ ,_filterComplete: function() {
208
+ this.trigger("filter-complete", this);
209
+ }
210
+ });
211
+ })(_, Backbone);
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backbone-filtered-collection
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dmitriy Likhten
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: '5.0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: '5.0'
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: jasmine
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ description: A filtered collection for backbone.js
69
+ email:
70
+ - dlikhten@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - .rvmrc
77
+ - Gemfile
78
+ - LICENSE
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - backbone-filtered-collection.gemspec
83
+ - lib/backbone-filtered-collection.rb
84
+ - lib/backbone/filtered_collection.rb
85
+ - lib/backbone/filtered_collection/engine.rb
86
+ - lib/backbone/filtered_collection/version.rb
87
+ - spec/javascripts/backbone-filtered-collection-spec.js
88
+ - spec/javascripts/dependencies/backbone.js
89
+ - spec/javascripts/dependencies/underscore.js
90
+ - spec/javascripts/helpers/SpecHelper.js
91
+ - spec/javascripts/support/jasmine.yml
92
+ - vendor/assets/javascripts/backbone-filtered-collection.js
93
+ homepage: http://github.com/dlikhten/filtered-collection
94
+ licenses:
95
+ - MIT
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Allowing implementation of a chain-of-responsibility pattern in backbone's
118
+ collection filtering
119
+ test_files:
120
+ - spec/javascripts/backbone-filtered-collection-spec.js
121
+ - spec/javascripts/dependencies/backbone.js
122
+ - spec/javascripts/dependencies/underscore.js
123
+ - spec/javascripts/helpers/SpecHelper.js
124
+ - spec/javascripts/support/jasmine.yml