"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/backbone/test/events.js" between
lodash-3.0.0.tar.gz and lodash-4.0.0.tar.gz

About: lodash is a modern JavaScript utility library delivering modularity, performance, & extras.

events.js  (lodash-3.0.0):events.js  (lodash-4.0.0)
(function() { (function() {
module("Backbone.Events"); QUnit.module('Backbone.Events');
test("on and trigger", 2, function() { QUnit.test('on and trigger', function(assert) {
var obj = { counter: 0 }; assert.expect(2);
_.extend(obj,Backbone.Events); var obj = {counter: 0};
_.extend(obj, Backbone.Events);
obj.on('event', function() { obj.counter += 1; }); obj.on('event', function() { obj.counter += 1; });
obj.trigger('event'); obj.trigger('event');
equal(obj.counter,1,'counter should be incremented.'); assert.equal(obj.counter, 1, 'counter should be incremented.');
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
equal(obj.counter, 5, 'counter should be incremented five times.'); assert.equal(obj.counter, 5, 'counter should be incremented five times.');
}); });
test("binding and triggering multiple events", 4, function() { QUnit.test('binding and triggering multiple events', function(assert) {
var obj = { counter: 0 }; assert.expect(4);
var obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
obj.on('a b c', function() { obj.counter += 1; }); obj.on('a b c', function() { obj.counter += 1; });
obj.trigger('a'); obj.trigger('a');
equal(obj.counter, 1); assert.equal(obj.counter, 1);
obj.trigger('a b'); obj.trigger('a b');
equal(obj.counter, 3); assert.equal(obj.counter, 3);
obj.trigger('c'); obj.trigger('c');
equal(obj.counter, 4); assert.equal(obj.counter, 4);
obj.off('a c'); obj.off('a c');
obj.trigger('a b c'); obj.trigger('a b c');
equal(obj.counter, 5); assert.equal(obj.counter, 5);
}); });
test("binding and triggering with event maps", function() { QUnit.test('binding and triggering with event maps', function(assert) {
var obj = { counter: 0 }; var obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
var increment = function() { var increment = function() {
this.counter += 1; this.counter += 1;
}; };
obj.on({ obj.on({
a: increment, a: increment,
b: increment, b: increment,
c: increment c: increment
}, obj); }, obj);
obj.trigger('a'); obj.trigger('a');
equal(obj.counter, 1); assert.equal(obj.counter, 1);
obj.trigger('a b'); obj.trigger('a b');
equal(obj.counter, 3); assert.equal(obj.counter, 3);
obj.trigger('c'); obj.trigger('c');
equal(obj.counter, 4); assert.equal(obj.counter, 4);
obj.off({ obj.off({
a: increment, a: increment,
c: increment c: increment
}, obj); }, obj);
obj.trigger('a b c'); obj.trigger('a b c');
equal(obj.counter, 5); assert.equal(obj.counter, 5);
}); });
test("listenTo and stopListening", 1, function() { QUnit.test('binding and triggering multiple event names with event maps', func
tion(assert) {
var obj = {counter: 0};
_.extend(obj, Backbone.Events);
var increment = function() {
this.counter += 1;
};
obj.on({
'a b c': increment
});
obj.trigger('a');
assert.equal(obj.counter, 1);
obj.trigger('a b');
assert.equal(obj.counter, 3);
obj.trigger('c');
assert.equal(obj.counter, 4);
obj.off({
'a c': increment
});
obj.trigger('a b c');
assert.equal(obj.counter, 5);
});
QUnit.test('binding and trigger with event maps context', function(assert) {
assert.expect(2);
var obj = {counter: 0};
var context = {};
_.extend(obj, Backbone.Events);
obj.on({
a: function() {
assert.strictEqual(this, context, 'defaults `context` to `callback` para
m');
}
}, context).trigger('a');
obj.off().on({
a: function() {
assert.strictEqual(this, context, 'will not override explicit `context`
param');
}
}, this, context).trigger('a');
});
QUnit.test('listenTo and stopListening', function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ ok(true); }); a.listenTo(b, 'all', function(){ assert.ok(true); });
b.trigger('anything'); b.trigger('anything');
a.listenTo(b, 'all', function(){ ok(false); }); a.listenTo(b, 'all', function(){ assert.ok(false); });
a.stopListening(); a.stopListening();
b.trigger('anything'); b.trigger('anything');
}); });
test("listenTo and stopListening with event maps", 4, function() { QUnit.test('listenTo and stopListening with event maps', function(assert) {
assert.expect(4);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
var cb = function(){ ok(true); }; var cb = function(){ assert.ok(true); };
a.listenTo(b, {event: cb}); a.listenTo(b, {event: cb});
b.trigger('event'); b.trigger('event');
a.listenTo(b, {event2: cb}); a.listenTo(b, {event2: cb});
b.on('event2', cb); b.on('event2', cb);
a.stopListening(b, {event2: cb}); a.stopListening(b, {event2: cb});
b.trigger('event event2'); b.trigger('event event2');
a.stopListening(); a.stopListening();
b.trigger('event event2'); b.trigger('event event2');
}); });
test("stopListening with omitted args", 2, function () { QUnit.test('stopListening with omitted args', function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
var cb = function () { ok(true); }; var cb = function() { assert.ok(true); };
a.listenTo(b, 'event', cb); a.listenTo(b, 'event', cb);
b.on('event', cb); b.on('event', cb);
a.listenTo(b, 'event2', cb); a.listenTo(b, 'event2', cb);
a.stopListening(null, {event: cb}); a.stopListening(null, {event: cb});
b.trigger('event event2'); b.trigger('event event2');
b.off(); b.off();
a.listenTo(b, 'event event2', cb); a.listenTo(b, 'event event2', cb);
a.stopListening(null, 'event'); a.stopListening(null, 'event');
a.stopListening(); a.stopListening();
b.trigger('event2'); b.trigger('event2');
}); });
test("listenToOnce and stopListening", 1, function() { QUnit.test('listenToOnce', function(assert) {
assert.expect(2);
// Same as the previous test, but we use once rather than having to explicit
ly unbind
var obj = {counterA: 0, counterB: 0};
_.extend(obj, Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.trigger('event'); };
var incrB = function(){ obj.counterB += 1; };
obj.listenToOnce(obj, 'event', incrA);
obj.listenToOnce(obj, 'event', incrB);
obj.trigger('event');
assert.equal(obj.counterA, 1, 'counterA should have only been incremented on
ce.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented on
ce.');
});
QUnit.test('listenToOnce and stopListening', function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function() { ok(true); }); a.listenToOnce(b, 'all', function() { assert.ok(true); });
b.trigger('anything'); b.trigger('anything');
b.trigger('anything'); b.trigger('anything');
a.listenToOnce(b, 'all', function() { ok(false); }); a.listenToOnce(b, 'all', function() { assert.ok(false); });
a.stopListening(); a.stopListening();
b.trigger('anything'); b.trigger('anything');
}); });
test("listenTo, listenToOnce and stopListening", 1, function() { QUnit.test('listenTo, listenToOnce and stopListening', function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function() { ok(true); }); a.listenToOnce(b, 'all', function() { assert.ok(true); });
b.trigger('anything'); b.trigger('anything');
b.trigger('anything'); b.trigger('anything');
a.listenTo(b, 'all', function() { ok(false); }); a.listenTo(b, 'all', function() { assert.ok(false); });
a.stopListening(); a.stopListening();
b.trigger('anything'); b.trigger('anything');
}); });
test("listenTo and stopListening with event maps", 1, function() { QUnit.test('listenTo and stopListening with event maps', function(assert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
a.listenTo(b, {change: function(){ ok(true); }}); a.listenTo(b, {change: function(){ assert.ok(true); }});
b.trigger('change'); b.trigger('change');
a.listenTo(b, {change: function(){ ok(false); }}); a.listenTo(b, {change: function(){ assert.ok(false); }});
a.stopListening(); a.stopListening();
b.trigger('change'); b.trigger('change');
}); });
test("listenTo yourself", 1, function(){ QUnit.test('listenTo yourself', function(assert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events); var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", function(){ ok(true); }); e.listenTo(e, 'foo', function(){ assert.ok(true); });
e.trigger("foo"); e.trigger('foo');
}); });
test("listenTo yourself cleans yourself up with stopListening", 1, function(){ QUnit.test('listenTo yourself cleans yourself up with stopListening', function
(assert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events); var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", function(){ ok(true); }); e.listenTo(e, 'foo', function(){ assert.ok(true); });
e.trigger("foo"); e.trigger('foo');
e.stopListening(); e.stopListening();
e.trigger("foo"); e.trigger('foo');
}); });
test("stopListening cleans up references", 4, function() { QUnit.test('stopListening cleans up references', function(assert) {
assert.expect(12);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
var fn = function() {}; var fn = function() {};
a.listenTo(b, 'all', fn).stopListening(); b.on('event', fn);
equal(_.size(a._listeningTo), 0); a.listenTo(b, 'event', fn).stopListening();
a.listenTo(b, 'all', fn).stopListening(b); assert.equal(_.size(a._listeningTo), 0);
equal(_.size(a._listeningTo), 0); assert.equal(_.size(b._events.event), 1);
a.listenTo(b, 'all', fn).stopListening(null, 'all'); assert.equal(_.size(b._listeners), 0);
equal(_.size(a._listeningTo), 0); a.listenTo(b, 'event', fn).stopListening(b);
a.listenTo(b, 'all', fn).stopListening(null, null, fn); assert.equal(_.size(a._listeningTo), 0);
equal(_.size(a._listeningTo), 0); assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn).stopListening(b, 'event');
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn).stopListening(b, 'event', fn);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
}); });
test("listenTo and stopListening cleaning up references", 2, function() { QUnit.test('stopListening cleans up references from listenToOnce', function(as
sert) {
assert.expect(12);
var a = _.extend({}, Backbone.Events); var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ ok(true); }); var fn = function() {};
b.on('event', fn);
a.listenToOnce(b, 'event', fn).stopListening();
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event');
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
a.listenToOnce(b, 'event', fn).stopListening(b, 'event', fn);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._events.event), 1);
assert.equal(_.size(b._listeners), 0);
});
QUnit.test('listenTo and off cleaning up references', function(assert) {
assert.expect(8);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var fn = function() {};
a.listenTo(b, 'event', fn);
b.off();
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off('event');
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off(null, fn);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
a.listenTo(b, 'event', fn);
b.off(null, null, a);
assert.equal(_.size(a._listeningTo), 0);
assert.equal(_.size(b._listeners), 0);
});
QUnit.test('listenTo and stopListening cleaning up references', function(asser
t) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ assert.ok(true); });
b.trigger('anything'); b.trigger('anything');
a.listenTo(b, 'other', function(){ ok(false); }); a.listenTo(b, 'other', function(){ assert.ok(false); });
a.stopListening(b, 'other'); a.stopListening(b, 'other');
a.stopListening(b, 'all'); a.stopListening(b, 'all');
equal(_.keys(a._listeningTo).length, 0); assert.equal(_.size(a._listeningTo), 0);
});
QUnit.test('listenToOnce without context cleans up references after the event
has fired', function(assert) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, 'all', function(){ assert.ok(true); });
b.trigger('anything');
assert.equal(_.size(a._listeningTo), 0);
});
QUnit.test('listenToOnce with event maps cleans up references', function(asser
t) {
assert.expect(2);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { assert.ok(true); },
two: function() { assert.ok(false); }
});
b.trigger('one');
assert.equal(_.size(a._listeningTo), 1);
});
QUnit.test('listenToOnce with event maps binds the correct `this`', function(a
ssert) {
assert.expect(1);
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenToOnce(b, {
one: function() { assert.ok(this === a); },
two: function() { assert.ok(false); }
});
b.trigger('one');
}); });
test("listenTo with empty callback doesn't throw an error", 1, function(){ QUnit.test("listenTo with empty callback doesn't throw an error", function(ass
ert) {
assert.expect(1);
var e = _.extend({}, Backbone.Events); var e = _.extend({}, Backbone.Events);
e.listenTo(e, "foo", null); e.listenTo(e, 'foo', null);
e.trigger("foo"); e.trigger('foo');
ok(true); assert.ok(true);
}); });
test("trigger all for each event", 3, function() { QUnit.test('trigger all for each event', function(assert) {
var a, b, obj = { counter: 0 }; assert.expect(3);
var a, b, obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
obj.on('all', function(event) { obj.on('all', function(event) {
obj.counter++; obj.counter++;
if (event == 'a') a = true; if (event == 'a') a = true;
if (event == 'b') b = true; if (event == 'b') b = true;
}) })
.trigger('a b'); .trigger('a b');
ok(a); assert.ok(a);
ok(b); assert.ok(b);
equal(obj.counter, 2); assert.equal(obj.counter, 2);
}); });
test("on, then unbind all functions", 1, function() { QUnit.test('on, then unbind all functions', function(assert) {
var obj = { counter: 0 }; assert.expect(1);
_.extend(obj,Backbone.Events); var obj = {counter: 0};
_.extend(obj, Backbone.Events);
var callback = function() { obj.counter += 1; }; var callback = function() { obj.counter += 1; };
obj.on('event', callback); obj.on('event', callback);
obj.trigger('event'); obj.trigger('event');
obj.off('event'); obj.off('event');
obj.trigger('event'); obj.trigger('event');
equal(obj.counter, 1, 'counter should have only been incremented once.'); assert.equal(obj.counter, 1, 'counter should have only been incremented once .');
}); });
test("bind two callbacks, unbind only one", 2, function() { QUnit.test('bind two callbacks, unbind only one', function(assert) {
var obj = { counterA: 0, counterB: 0 }; assert.expect(2);
_.extend(obj,Backbone.Events); var obj = {counterA: 0, counterB: 0};
_.extend(obj, Backbone.Events);
var callback = function() { obj.counterA += 1; }; var callback = function() { obj.counterA += 1; };
obj.on('event', callback); obj.on('event', callback);
obj.on('event', function() { obj.counterB += 1; }); obj.on('event', function() { obj.counterB += 1; });
obj.trigger('event'); obj.trigger('event');
obj.off('event', callback); obj.off('event', callback);
obj.trigger('event'); obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.'); assert.equal(obj.counterA, 1, 'counterA should have only been incremented on
equal(obj.counterB, 2, 'counterB should have been incremented twice.'); ce.');
assert.equal(obj.counterB, 2, 'counterB should have been incremented twice.'
);
}); });
test("unbind a callback in the midst of it firing", 1, function() { QUnit.test('unbind a callback in the midst of it firing', function(assert) {
assert.expect(1);
var obj = {counter: 0}; var obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
var callback = function() { var callback = function() {
obj.counter += 1; obj.counter += 1;
obj.off('event', callback); obj.off('event', callback);
}; };
obj.on('event', callback); obj.on('event', callback);
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
equal(obj.counter, 1, 'the callback should have been unbound.'); assert.equal(obj.counter, 1, 'the callback should have been unbound.');
}); });
test("two binds that unbind themeselves", 2, function() { QUnit.test('two binds that unbind themeselves', function(assert) {
var obj = { counterA: 0, counterB: 0 }; assert.expect(2);
_.extend(obj,Backbone.Events); var obj = {counterA: 0, counterB: 0};
_.extend(obj, Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.off('event', incrA); }; var incrA = function(){ obj.counterA += 1; obj.off('event', incrA); };
var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); }; var incrB = function(){ obj.counterB += 1; obj.off('event', incrB); };
obj.on('event', incrA); obj.on('event', incrA);
obj.on('event', incrB); obj.on('event', incrB);
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.'); assert.equal(obj.counterA, 1, 'counterA should have only been incremented on
equal(obj.counterB, 1, 'counterB should have only been incremented once.'); ce.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented on
ce.');
}); });
test("bind a callback with a supplied context", 1, function () { QUnit.test('bind a callback with a supplied context', function(assert) {
var TestClass = function () { assert.expect(1);
var TestClass = function() {
return this; return this;
}; };
TestClass.prototype.assertTrue = function () { TestClass.prototype.assertTrue = function() {
ok(true, '`this` was bound to the callback'); assert.ok(true, '`this` was bound to the callback');
}; };
var obj = _.extend({},Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.on('event', function () { this.assertTrue(); }, (new TestClass)); obj.on('event', function() { this.assertTrue(); }, new TestClass);
obj.trigger('event'); obj.trigger('event');
}); });
test("nested trigger with unbind", 1, function () { QUnit.test('nested trigger with unbind', function(assert) {
var obj = { counter: 0 }; assert.expect(1);
var obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
var incr1 = function(){ obj.counter += 1; obj.off('event', incr1); obj.trigg er('event'); }; var incr1 = function(){ obj.counter += 1; obj.off('event', incr1); obj.trigg er('event'); };
var incr2 = function(){ obj.counter += 1; }; var incr2 = function(){ obj.counter += 1; };
obj.on('event', incr1); obj.on('event', incr1);
obj.on('event', incr2); obj.on('event', incr2);
obj.trigger('event'); obj.trigger('event');
equal(obj.counter, 3, 'counter should have been incremented three times'); assert.equal(obj.counter, 3, 'counter should have been incremented three tim es');
}); });
test("callback list is not altered during trigger", 2, function () { QUnit.test('callback list is not altered during trigger', function(assert) {
assert.expect(2);
var counter = 0, obj = _.extend({}, Backbone.Events); var counter = 0, obj = _.extend({}, Backbone.Events);
var incr = function(){ counter++; }; var incr = function(){ counter++; };
obj.on('event', function(){ obj.on('event', incr).on('all', incr); }) var incrOn = function(){ obj.on('event all', incr); };
.trigger('event'); var incrOff = function(){ obj.off('event all', incr); };
equal(counter, 0, 'bind does not alter callback list');
obj.off() obj.on('event all', incrOn).trigger('event');
.on('event', function(){ obj.off('event', incr).off('all', incr); }) assert.equal(counter, 0, 'on does not alter callback list');
.on('event', incr)
.on('all', incr) obj.off().on('event', incrOff).on('event all', incr).trigger('event');
.trigger('event'); assert.equal(counter, 2, 'off does not alter callback list');
equal(counter, 2, 'unbind does not alter callback list');
}); });
test("#1282 - 'all' callback list is retrieved after each event.", 1, function QUnit.test("#1282 - 'all' callback list is retrieved after each event.", funct
() { ion(assert) {
assert.expect(1);
var counter = 0; var counter = 0;
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
var incr = function(){ counter++; }; var incr = function(){ counter++; };
obj.on('x', function() { obj.on('x', function() {
obj.on('y', incr).on('all', incr); obj.on('y', incr).on('all', incr);
}) })
.trigger('x y'); .trigger('x y');
strictEqual(counter, 2); assert.strictEqual(counter, 2);
}); });
test("if no callback is provided, `on` is a noop", 0, function() { QUnit.test('if no callback is provided, `on` is a noop', function(assert) {
assert.expect(0);
_.extend({}, Backbone.Events).on('test').trigger('test'); _.extend({}, Backbone.Events).on('test').trigger('test');
}); });
test("if callback is truthy but not a function, `on` should throw an error jus QUnit.test('if callback is truthy but not a function, `on` should throw an err
t like jQuery", 1, function() { or just like jQuery', function(assert) {
assert.expect(1);
var view = _.extend({}, Backbone.Events).on('test', 'noop'); var view = _.extend({}, Backbone.Events).on('test', 'noop');
raises(function() { assert.throws(function() {
view.trigger('test'); view.trigger('test');
}); });
}); });
test("remove all events for a specific context", 4, function() { QUnit.test('remove all events for a specific context', function(assert) {
assert.expect(4);
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.on('x y all', function() { ok(true); }); obj.on('x y all', function() { assert.ok(true); });
obj.on('x y all', function() { ok(false); }, obj); obj.on('x y all', function() { assert.ok(false); }, obj);
obj.off(null, null, obj); obj.off(null, null, obj);
obj.trigger('x y'); obj.trigger('x y');
}); });
test("remove all events for a specific callback", 4, function() { QUnit.test('remove all events for a specific callback', function(assert) {
assert.expect(4);
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
var success = function() { ok(true); }; var success = function() { assert.ok(true); };
var fail = function() { ok(false); }; var fail = function() { assert.ok(false); };
obj.on('x y all', success); obj.on('x y all', success);
obj.on('x y all', fail); obj.on('x y all', fail);
obj.off(null, fail); obj.off(null, fail);
obj.trigger('x y'); obj.trigger('x y');
}); });
test("#1310 - off does not skip consecutive events", 0, function() { QUnit.test('#1310 - off does not skip consecutive events', function(assert) {
assert.expect(0);
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.on('event', function() { ok(false); }, obj); obj.on('event', function() { assert.ok(false); }, obj);
obj.on('event', function() { ok(false); }, obj); obj.on('event', function() { assert.ok(false); }, obj);
obj.off(null, null, obj); obj.off(null, null, obj);
obj.trigger('event'); obj.trigger('event');
}); });
test("once", 2, function() { QUnit.test('once', function(assert) {
assert.expect(2);
// Same as the previous test, but we use once rather than having to explicit ly unbind // Same as the previous test, but we use once rather than having to explicit ly unbind
var obj = { counterA: 0, counterB: 0 }; var obj = {counterA: 0, counterB: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.trigger('event'); }; var incrA = function(){ obj.counterA += 1; obj.trigger('event'); };
var incrB = function(){ obj.counterB += 1; }; var incrB = function(){ obj.counterB += 1; };
obj.once('event', incrA); obj.once('event', incrA);
obj.once('event', incrB); obj.once('event', incrB);
obj.trigger('event'); obj.trigger('event');
equal(obj.counterA, 1, 'counterA should have only been incremented once.'); assert.equal(obj.counterA, 1, 'counterA should have only been incremented on
equal(obj.counterB, 1, 'counterB should have only been incremented once.'); ce.');
assert.equal(obj.counterB, 1, 'counterB should have only been incremented on
ce.');
}); });
test("once variant one", 3, function() { QUnit.test('once variant one', function(assert) {
var f = function(){ ok(true); }; assert.expect(3);
var f = function(){ assert.ok(true); };
var a = _.extend({}, Backbone.Events).once('event', f); var a = _.extend({}, Backbone.Events).once('event', f);
var b = _.extend({}, Backbone.Events).on('event', f); var b = _.extend({}, Backbone.Events).on('event', f);
a.trigger('event'); a.trigger('event');
b.trigger('event'); b.trigger('event');
b.trigger('event'); b.trigger('event');
}); });
test("once variant two", 3, function() { QUnit.test('once variant two', function(assert) {
var f = function(){ ok(true); }; assert.expect(3);
var f = function(){ assert.ok(true); };
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj obj
.once('event', f) .once('event', f)
.on('event', f) .on('event', f)
.trigger('event') .trigger('event')
.trigger('event'); .trigger('event');
}); });
test("once with off", 0, function() { QUnit.test('once with off', function(assert) {
var f = function(){ ok(true); }; assert.expect(0);
var f = function(){ assert.ok(true); };
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.once('event', f); obj.once('event', f);
obj.off('event', f); obj.off('event', f);
obj.trigger('event'); obj.trigger('event');
}); });
test("once with event maps", function() { QUnit.test('once with event maps', function(assert) {
var obj = { counter: 0 }; var obj = {counter: 0};
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
var increment = function() { var increment = function() {
this.counter += 1; this.counter += 1;
}; };
obj.once({ obj.once({
a: increment, a: increment,
b: increment, b: increment,
c: increment c: increment
}, obj); }, obj);
obj.trigger('a'); obj.trigger('a');
equal(obj.counter, 1); assert.equal(obj.counter, 1);
obj.trigger('a b'); obj.trigger('a b');
equal(obj.counter, 2); assert.equal(obj.counter, 2);
obj.trigger('c'); obj.trigger('c');
equal(obj.counter, 3); assert.equal(obj.counter, 3);
obj.trigger('a b c'); obj.trigger('a b c');
equal(obj.counter, 3); assert.equal(obj.counter, 3);
}); });
test("once with off only by context", 0, function() { QUnit.test('once with off only by context', function(assert) {
assert.expect(0);
var context = {}; var context = {};
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.once('event', function(){ ok(false); }, context); obj.once('event', function(){ assert.ok(false); }, context);
obj.off(null, null, context); obj.off(null, null, context);
obj.trigger('event'); obj.trigger('event');
}); });
test("Backbone object inherits Events", function() { QUnit.test('Backbone object inherits Events', function(assert) {
ok(Backbone.on === Backbone.Events.on); assert.ok(Backbone.on === Backbone.Events.on);
}); });
asyncTest("once with asynchronous events", 1, function() { QUnit.test('once with asynchronous events', function(assert) {
var func = _.debounce(function() { ok(true); start(); }, 50); var done = assert.async();
assert.expect(1);
var func = _.debounce(function() { assert.ok(true); done(); }, 50);
var obj = _.extend({}, Backbone.Events).once('async', func); var obj = _.extend({}, Backbone.Events).once('async', func);
obj.trigger('async'); obj.trigger('async');
obj.trigger('async'); obj.trigger('async');
}); });
test("once with multiple events.", 2, function() { QUnit.test('once with multiple events.', function(assert) {
assert.expect(2);
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
obj.once('x y', function() { ok(true); }); obj.once('x y', function() { assert.ok(true); });
obj.trigger('x y'); obj.trigger('x y');
}); });
test("Off during iteration with once.", 2, function() { QUnit.test('Off during iteration with once.', function(assert) {
assert.expect(2);
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
var f = function(){ this.off('event', f); }; var f = function(){ this.off('event', f); };
obj.on('event', f); obj.on('event', f);
obj.once('event', function(){}); obj.once('event', function(){});
obj.on('event', function(){ ok(true); }); obj.on('event', function(){ assert.ok(true); });
obj.trigger('event'); obj.trigger('event');
obj.trigger('event'); obj.trigger('event');
}); });
test("`once` on `all` should work as expected", 1, function() { QUnit.test('`once` on `all` should work as expected', function(assert) {
assert.expect(1);
Backbone.once('all', function() { Backbone.once('all', function() {
ok(true); assert.ok(true);
Backbone.trigger('all'); Backbone.trigger('all');
}); });
Backbone.trigger('all'); Backbone.trigger('all');
}); });
test("once without a callback is a noop", 0, function() { QUnit.test('once without a callback is a noop', function(assert) {
assert.expect(0);
_.extend({}, Backbone.Events).once('event').trigger('event'); _.extend({}, Backbone.Events).once('event').trigger('event');
}); });
test("event functions are chainable", function() { QUnit.test('listenToOnce without a callback is a noop', function(assert) {
assert.expect(0);
var obj = _.extend({}, Backbone.Events);
obj.listenToOnce(obj, 'event').trigger('event');
});
QUnit.test('event functions are chainable', function(assert) {
var obj = _.extend({}, Backbone.Events); var obj = _.extend({}, Backbone.Events);
var obj2 = _.extend({}, Backbone.Events); var obj2 = _.extend({}, Backbone.Events);
var fn = function() {}; var fn = function() {};
equal(obj, obj.trigger('noeventssetyet')); assert.equal(obj, obj.trigger('noeventssetyet'));
equal(obj, obj.off('noeventssetyet')); assert.equal(obj, obj.off('noeventssetyet'));
equal(obj, obj.stopListening('noeventssetyet')); assert.equal(obj, obj.stopListening('noeventssetyet'));
equal(obj, obj.on('a', fn)); assert.equal(obj, obj.on('a', fn));
equal(obj, obj.once('c', fn)); assert.equal(obj, obj.once('c', fn));
equal(obj, obj.trigger('a')); assert.equal(obj, obj.trigger('a'));
equal(obj, obj.listenTo(obj2, 'a', fn)); assert.equal(obj, obj.listenTo(obj2, 'a', fn));
equal(obj, obj.listenToOnce(obj2, 'b', fn)); assert.equal(obj, obj.listenToOnce(obj2, 'b', fn));
equal(obj, obj.off('a c')); assert.equal(obj, obj.off('a c'));
equal(obj, obj.stopListening(obj2, 'a')); assert.equal(obj, obj.stopListening(obj2, 'a'));
equal(obj, obj.stopListening()); assert.equal(obj, obj.stopListening());
});
QUnit.test('#3448 - listenToOnce with space-separated events', function(assert
) {
assert.expect(2);
var one = _.extend({}, Backbone.Events);
var two = _.extend({}, Backbone.Events);
var count = 1;
one.listenToOnce(two, 'x y', function(n) { assert.ok(n === count++); });
two.trigger('x', 1);
two.trigger('x', 1);
two.trigger('y', 2);
two.trigger('y', 2);
}); });
})(); })();
 End of changes. 95 change blocks. 
155 lines changed or deleted 379 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)