Implement once with minimal change to other Events methods.

This commit is contained in:
Brad Dunbar
2012-12-07 18:59:58 -05:00
parent 615b08c9e8
commit 1be8eb0847
2 changed files with 44 additions and 17 deletions

View File

@@ -227,7 +227,7 @@ $(document).ready(function() {
test("once", 2, function() {
// Same as the previous test, but we use once rather than having to explicitly unbind
var obj = { counterA: 0, counterB: 0 };
_.extend(obj,Backbone.Events);
_.extend(obj, Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.trigger('event'); };
var incrB = function(){ obj.counterB += 1 };
obj.once('event', incrA);
@@ -310,4 +310,21 @@ $(document).ready(function() {
obj.trigger('async');
});
test("Off during iteration with once.", 2, function() {
var obj = _.extend({}, Backbone.Events);
var f = function(){ this.off('event', f); };
obj.on('event', f);
obj.once('event', function(){});
obj.on('event', function(){ ok(true); });
obj.trigger('event');
obj.trigger('event');
});
test("once with multiple events.", 2, function() {
var obj = _.extend({}, Backbone.Events);
obj.once('x y', function() { ok(true); });
obj.trigger('x y');
});
});