Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow a one-time event listener on Ember.Evented
This commit allows you to listen for an event
once. Once the event fires, Ember immediately
unbinds it.

The API is modeled after jQuery's .one method.
  • Loading branch information
Tomhuda Katzdale committed Jun 18, 2012
1 parent 332d214 commit 1809e65
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/ember-runtime/lib/mixins/evented.js
Expand Up @@ -16,6 +16,25 @@ Ember.Evented = Ember.Mixin.create({
Ember.addListener(this, name, target, method, xform); Ember.addListener(this, name, target, method, xform);
}, },


one: function(name, target, method) {
if (!method) {
method = target;
target = null;
}

var wrapped = function() {
Ember.removeListener(this, name, target, wrapped);

// Internally, a `null` target means that the target is
// the first parameter to addListener. That means that
// the `this` passed into this function is the target
// determined by the event system.
method.apply(this, arguments);
};

this.on(name, target, wrapped);
},

trigger: function(name) { trigger: function(name) {
Ember.sendEvent.apply(null, [this, name].concat(a_slice.call(arguments, 1))); Ember.sendEvent.apply(null, [this, name].concat(a_slice.call(arguments, 1)));
}, },
Expand Down
40 changes: 40 additions & 0 deletions packages/ember-runtime/tests/system/object/events_test.js
Expand Up @@ -16,6 +16,22 @@ test("a listener can be added to an object", function() {
equal(count, 2, "the event was triggered"); equal(count, 2, "the event was triggered");
}); });


test("a listener can be added and removed automatically the first time it is triggerd", function() {
var count = 0;
var F = function() { count++; };

var obj = Ember.Object.create(Ember.Evented);

obj.one('event!', F);
obj.trigger('event!');

equal(count, 1, "the event was triggered");

obj.trigger('event!');

equal(count, 1, "the event was not triggered again");
});

test("triggering an event can have arguments", function() { test("triggering an event can have arguments", function() {
var self, args; var self, args;


Expand All @@ -32,6 +48,30 @@ test("triggering an event can have arguments", function() {
equal(self, obj); equal(self, obj);
}); });


test("a listener can be added and removed automatically and have arguments", function() {
var self, args, count = 0;

var obj = Ember.Object.create(Ember.Evented);

obj.one('event!', function() {
args = [].slice.call(arguments);
self = this;
count++;
});

obj.trigger('event!', "foo", "bar");

deepEqual(args, [ "foo", "bar" ]);
equal(self, obj);
equal(count, 1, "the event is triggered once");

obj.trigger('event!', "baz", "bat");

deepEqual(args, [ "foo", "bar" ]);
equal(count, 1, "the event was not triggered again");
equal(self, obj);
});

test("binding an event can specify a different target", function() { test("binding an event can specify a different target", function() {
var self, args; var self, args;


Expand Down

1 comment on commit 1809e65

@josepjaume
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! :D

Please sign in to comment.