Skip to content

Commit

Permalink
Prefer on/off over bind/unbind.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Jun 3, 2012
1 parent 465f297 commit 7828d6d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
30 changes: 15 additions & 15 deletions test/collection.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
Expand All @@ -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([]);
Expand All @@ -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]);
Expand All @@ -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) {
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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'; }
});
Expand Down
32 changes: 16 additions & 16 deletions test/events.js
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -110,31 +110,31 @@ $(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');
});

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');
});
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion test/router.js
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions test/speed.js
Expand Up @@ -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);
Expand All @@ -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()});
});

})();
})();

0 comments on commit 7828d6d

Please sign in to comment.