Permalink
Browse files

Implement destroyWith via @ronen

  • Loading branch information...
1 parent 1cb4c56 commit d6f896730b06565285b826b86f7871352fb6a28c @p3drosola committed Jul 20, 2014
Showing with 22 additions and 29 deletions.
  1. +5 −6 README.md
  2. +5 −4 backbone.virtual-collection.js
  3. +12 −19 test/spec.js
View
@@ -26,8 +26,6 @@ Every effort has been made to maintain compatibility with v0.4, and in most case
**Backbone.VirtualCollection** is now VirtualCollection. It's cleaner to use a global variable, instead of attaching onto Backbone. It works better in various js environments (Bower, AMD, CommonJS, etc).
-
-
### Usage
For example, let's say you have a task collection, and want to show a list of tasks that belong to a specific user.
@@ -75,24 +73,25 @@ virtual_collection.sort(); // triggers sort event
#### Unbinding
The virtual collection will keep listening to its parent collection until you call `stopListening`.
-You can use the helper function `virtual_collection.closeWith` to tell the collection to stopListening when a Marionette view is closed.
+You can use the helper function `virtual_collection.destroyWith` to tell the collection to stopListening when a Marionette view is destroyed.
```js
var virtual_collection = new Backbone.VirtualCollection(collection, {filter: {foo: 'bar'}});
var view = new Marionette.CollectionView({collection: virtual_collection});
-virtual_collection.closeWith(view);
+virtual_collection.destroyWith(view);
```
Using the helper will take care of unbinding the virtual collection's listeners when the view is closed.
-You also can pass a `close_with` option when creating the virtual collection being that an event emitter. The virtual collection will stop listening to events when the `close_with` event emitter emits a `close` event.
+You also can pass a `destroy_with` option when creating the virtual collection being that an event emitter. The virtual collection will stop listening to events when the `destroy_with` event emitter emits a `destroy` event.
```js
var virtual_collection = new Backbone.VirtualCollection(collection, {
filter: {foo: 'bar'},
- close_with: view
+ destroy_with: view
});
```
++**Note:** Prior to Marionette 2.*, "destroy" was called "close". For compatibility with older versions of Marionette, the old helper `virtual_collection.closeWith` and option `close_with` are still availble, handling the `close` event.
#### Update filter
@@ -25,7 +25,8 @@
this.collection = collection;
if (options.comparator !== undefined) this.comparator = options.comparator;
- if (options.close_with) this.closeWith(options.close_with);
+ if (options.close_with) this.bindLifecycle(options.close_with, 'close'); // Marionette 1.*
+ if (options.destroy_with) this.bindLifecycle(options.destroy_with, 'destroy'); // Marionette 2.*
if (!this.model) this.model = collection.model;
this.accepts = VirtualCollection.buildFilter(options.filter);
@@ -39,9 +40,9 @@
this.initialize.apply(this, arguments);
},
- // marionette specific
- closeWith: function (view) {
- view.on('close', _.bind(this.stopListening, this));
+ // Marionette 1.*
+ bindLifecycle: function (view, method_name) {
+ view.on(method_name, _.bind(this.stopListening, this));
},
updateFilter: function (filter) {
View
@@ -38,14 +38,25 @@ describe('Backbone.VirtualCollection', function () {
assert.equal(vc.models.length, 2);
});
- it('should accept a close_with option and bind to the `close` event', function () {
+ it('should accept a close_with option and bind to the `close` event (Marionette 1.*)', function () {
var vc, calls, collection, event_emitter;
collection = new Backbone.Collection([{id: 1, foo: 'bar'}]);
event_emitter = Backbone.Events;
sinon.spy(event_emitter, 'on');
vc = new VirtualCollection(collection, {close_with: event_emitter});
calls = JSON.stringify(_.map(event_emitter.on.args, function (i) {return i[0]; }));
assert.equal(calls, JSON.stringify([ 'close' ]));
+ event_emitter.on.restore()
+ });
+ it("should accept a destroy_with option and bind destroy event (Marionette 2.*)", function () {
+ var vc, calls, collection, event_emitter;
+ collection = new Backbone.Collection([{id: 1, foo: 'bar'}]);
+ event_emitter = Backbone.Events;
+ sinon.spy(event_emitter, 'on');
+ vc = new VirtualCollection(collection, {destroy_with: event_emitter});
+ calls = JSON.stringify(_.map(event_emitter.on.args, function (i) {return i[0]; }));
+ assert.equal(calls, JSON.stringify([ 'destroy' ]));
+ event_emitter.on.restore()
});
});
@@ -252,24 +263,6 @@ describe('Backbone.VirtualCollection', function () {
assert.equal(vc.length, collection.length);
})
});
-
- describe('closeWith', function () {
- it('should clear the virtual collection\'s listeners when the view is closed', function () {
- var collection = new Backbone.Collection([
- {id: 1, name: 'ccc'},
- {id: 2, name: 'bbb'},
- {id: 3, name: 'aaa'}
- ]),
- view = _.extend({}, Backbone.Events),
- vc = new VirtualCollection(collection);
-
- sinon.spy(vc, 'stopListening');
- vc.closeWith(view);
- view.trigger('close');
- assert.equal(vc.stopListening.callCount, 1);
- });
- });
-
describe('map', function () {
it('should map the models in the virtual collection', function () {
var collection = new Backbone.Collection([

0 comments on commit d6f8967

Please sign in to comment.