From 7828d6d22d625144dc29de06dc6bbd74b9ca0489 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Sun, 3 Jun 2012 15:06:44 -0400 Subject: [PATCH] Prefer on/off over bind/unbind. --- test/collection.js | 30 +++++++++++++++--------------- test/events.js | 32 ++++++++++++++++---------------- test/router.js | 2 +- test/speed.js | 16 ++++++++-------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/test/collection.js b/test/collection.js index e7c643f9..33f01924 100644 --- a/test/collection.js +++ b/test/collection.js @@ -91,10 +91,10 @@ $(document).ready(function() { added = opts = secondAdded = null; e = new Backbone.Model({id: 10, label : 'e'}); otherCol.add(e); - otherCol.bind('add', function() { + otherCol.on('add', function() { secondAdded = true; }); - col.bind('add', function(model, collection, options){ + col.on('add', function(model, collection, options){ added = model.get('label'); equal(options.index, 4); opts = options; @@ -163,7 +163,7 @@ $(document).ready(function() { test("Collection: add model to multiple collections", 10, function() { var counter = 0; var e = new Backbone.Model({id: 10, label : 'e'}); - e.bind('add', function(model, collection) { + e.on('add', function(model, collection) { counter++; equal(e, model); if (counter > 1) { @@ -173,12 +173,12 @@ $(document).ready(function() { } }); var colE = new Backbone.Collection([]); - colE.bind('add', function(model, collection) { + colE.on('add', function(model, collection) { equal(e, model); equal(colE, collection); }); var colF = new Backbone.Collection([]); - colF.bind('add', function(model, collection) { + colF.on('add', function(model, collection) { equal(e, model); equal(colF, collection); }); @@ -233,11 +233,11 @@ $(document).ready(function() { test("Collection: remove", 5, function() { var removed = null; var otherRemoved = null; - col.bind('remove', function(model, col, options) { + col.on('remove', function(model, col, options) { removed = model.get('label'); equal(options.index, 3); }); - otherCol.bind('remove', function(model, col, options) { + otherCol.on('remove', function(model, col, options) { otherRemoved = true; }); col.remove(d); @@ -264,7 +264,7 @@ $(document).ready(function() { var counter = 0; var dj = new Backbone.Model(); var emcees = new Backbone.Collection([dj]); - emcees.bind('change', function(){ counter++; }); + emcees.on('change', function(){ counter++; }); dj.set({name : 'Kool'}); equal(counter, 1); emcees.reset([]); @@ -281,7 +281,7 @@ $(document).ready(function() { var passed = false; var e = new Backbone.Model(modelData); var f = new Backbone.Model(modelData); - f.bind('remove', function() { + f.on('remove', function() { passed = true; }); var colE = new Backbone.Collection([e]); @@ -300,7 +300,7 @@ $(document).ready(function() { test("Collection: remove same model in multiple collection", 16, function() { var counter = 0; var e = new Backbone.Model({id: 5, title: 'Othello'}); - e.bind('remove', function(model, collection) { + e.on('remove', function(model, collection) { counter++; equal(e, model); if (counter > 1) { @@ -310,12 +310,12 @@ $(document).ready(function() { } }); var colE = new Backbone.Collection([e]); - colE.bind('remove', function(model, collection) { + colE.on('remove', function(model, collection) { equal(e, model); equal(colE, collection); }); var colF = new Backbone.Collection([e]); - colF.bind('remove', function(model, collection) { + colF.on('remove', function(model, collection) { equal(e, model); equal(colF, collection); }); @@ -453,7 +453,7 @@ $(document).ready(function() { test("Collection: reset", 10, function() { var resetCount = 0; var models = col.models; - col.bind('reset', function() { resetCount += 1; }); + col.on('reset', function() { resetCount += 1; }); col.reset([]); equal(resetCount, 1); equal(col.length, 0); @@ -485,7 +485,7 @@ $(document).ready(function() { test("Collection: trigger custom events on models", 1, function() { var fired = null; - a.bind("custom", function() { fired = true; }); + a.on("custom", function() { fired = true; }); a.trigger("custom"); equal(fired, true); }); @@ -559,7 +559,7 @@ $(document).ready(function() { test("Collection: throwing during add leaves consistent state", 4, function() { var col = new Backbone.Collection(); - col.bind('test', function() { ok(false); }); + col.on('test', function() { ok(false); }); col.model = Backbone.Model.extend({ validate: function(attrs){ if (!attrs.valid) return 'invalid'; } }); diff --git a/test/events.js b/test/events.js index 545875be..6d597743 100644 --- a/test/events.js +++ b/test/events.js @@ -78,9 +78,9 @@ $(document).ready(function() { _.extend(obj, Backbone.Events); var callback = function() { obj.counter += 1; - obj.unbind('event', callback); + obj.off('event', callback); }; - obj.bind('event', callback); + obj.on('event', callback); obj.trigger('event'); obj.trigger('event'); obj.trigger('event'); @@ -90,10 +90,10 @@ $(document).ready(function() { test("Events: two binds that unbind themeselves", 2, function() { var obj = { counterA: 0, counterB: 0 }; _.extend(obj,Backbone.Events); - var incrA = function(){ obj.counterA += 1; obj.unbind('event', incrA); }; - var incrB = function(){ obj.counterB += 1; obj.unbind('event', incrB); }; - obj.bind('event', incrA); - obj.bind('event', incrB); + var incrA = function(){ obj.counterA += 1; obj.off('event', incrA); }; + var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); }; + obj.on('event', incrA); + obj.on('event', incrB); obj.trigger('event'); obj.trigger('event'); obj.trigger('event'); @@ -110,17 +110,17 @@ $(document).ready(function() { }; var obj = _.extend({},Backbone.Events); - obj.bind('event', function () { this.assertTrue(); }, (new TestClass)); + obj.on('event', function () { this.assertTrue(); }, (new TestClass)); obj.trigger('event'); }); test("Events: nested trigger with unbind", 1, function () { var obj = { counter: 0 }; _.extend(obj, Backbone.Events); - var incr1 = function(){ obj.counter += 1; obj.unbind('event', incr1); obj.trigger('event'); }; + var incr1 = function(){ obj.counter += 1; obj.off('event', incr1); obj.trigger('event'); }; var incr2 = function(){ obj.counter += 1; }; - obj.bind('event', incr1); - obj.bind('event', incr2); + obj.on('event', incr1); + obj.on('event', incr2); obj.trigger('event'); equal(obj.counter, 3, 'counter should have been incremented three times'); }); @@ -128,13 +128,13 @@ $(document).ready(function() { test("Events: callback list is not altered during trigger", 2, function () { var counter = 0, obj = _.extend({}, Backbone.Events); var incr = function(){ counter++; }; - obj.bind('event', function(){ obj.bind('event', incr).bind('all', incr); }) + obj.on('event', function(){ obj.on('event', incr).on('all', incr); }) .trigger('event'); equal(counter, 0, 'bind does not alter callback list'); - obj.unbind() - .bind('event', function(){ obj.unbind('event', incr).unbind('all', incr); }) - .bind('event', incr) - .bind('all', incr) + obj.off() + .on('event', function(){ obj.off('event', incr).off('all', incr); }) + .on('event', incr) + .on('all', incr) .trigger('event'); equal(counter, 2, 'unbind does not alter callback list'); }); @@ -151,7 +151,7 @@ $(document).ready(function() { }); test("if no callback is provided, `on` is a noop", 0, function() { - _.extend({}, Backbone.Events).bind('test').trigger('test'); + _.extend({}, Backbone.Events).on('test').trigger('test'); }); test("remove all events for a specific context", 4, function() { diff --git a/test/router.js b/test/router.js index ede7032d..68106b4b 100644 --- a/test/router.js +++ b/test/router.js @@ -226,7 +226,7 @@ $(document).ready(function() { try{ var callbackFired = false; var myCallback = function(){ callbackFired = true; }; - router.bind("route:noCallback", myCallback); + router.on("route:noCallback", myCallback); window.location.hash = "noCallback"; setTimeout(function(){ equal(callbackFired, true); diff --git a/test/speed.js b/test/speed.js index 2c62c16a..db0a2c8d 100644 --- a/test/speed.js +++ b/test/speed.js @@ -5,18 +5,18 @@ var fn = function(){}; JSLitmus.test('Events: bind + unbind', function() { - object.bind("event", fn); - object.unbind("event", fn); + object.on("event", fn); + object.off("event", fn); }); - object.bind('test:trigger', fn); + object.on('test:trigger', fn); JSLitmus.test('Events: trigger', function() { object.trigger('test:trigger'); }); - object.bind('test:trigger2', fn); - object.bind('test:trigger2', fn); + object.on('test:trigger2', fn); + object.on('test:trigger2', fn); JSLitmus.test('Events: trigger 2, passing 5 args', function() { object.trigger('test:trigger2', 1, 2, 3, 4, 5); @@ -29,17 +29,17 @@ }); var eventModel = new Backbone.Model; - eventModel.bind('change', fn); + eventModel.on('change', fn); JSLitmus.test('Model: set rand() with an event', function() { eventModel.set({number: Math.random()}); }); var keyModel = new Backbone.Model; - keyModel.bind('change:number', fn); + keyModel.on('change:number', fn); JSLitmus.test('Model: set rand() with an attribute observer', function() { keyModel.set({number: Math.random()}); }); -})(); \ No newline at end of file +})();