Skip to content

Commit

Permalink
feat(component): add once method (naver#434)
Browse files Browse the repository at this point in the history
once is executed event just one time.

Ref naver#412
Close naver#412
  • Loading branch information
mixed authored Jan 3, 2017
1 parent 356857b commit 62f456e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,51 @@ eg.module("component", [eg], function(ns) {

return !isCanceled;
},
/**
* Executed event just one time.
* @ko 이벤트가 한번만 실행된다.
* @method eg.Component#once
* @param {eventName} eventName The name of the event to be attached <ko>등록할 이벤트의 이름</ko>
* @param {Function} handlerToAttach The handler function of the event to be attached <ko>등록할 이벤트의 핸들러 함수</ko>
* @return {eg.Component} An instance of a component itself<ko>컴포넌트 자신의 인스턴스</ko>
* @example
var Some = eg.Class.extend(eg.Component,{
"hi": function(){
alert("hi");
},
"thing": function(){
this.once("hi",this.hi);
}
});
var some = new Some();
some.thing();
some.trigger("hi");
// fire alert("hi");
some.trigger("hi");
// Nothing happens
*/
once: function(eventName, handlerToAttach) {
if (typeof eventName === "object" &&
typeof handlerToAttach === "undefined") {
var eventHash = eventName;
var i;
for (i in eventHash) {
this.once(i, eventHash[i]);
}
return this;
} else if (typeof eventName === "string" &&
typeof handlerToAttach === "function") {
var self = this;
this.on(eventName, function listener() {
var arg = Array.prototype.slice.call(arguments);
handlerToAttach.apply(self, arg);
self.off(eventName, listener);
});
}

return this;
},
/**
* Checks whether an event has been attached to a component.
* @ko 컴포넌트에 이벤트가 등록됐는지 확인한다.
Expand Down
74 changes: 73 additions & 1 deletion test/unit/js/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,76 @@ test("The error should not occur without initialization of options property in S
}
//Then
ok(result);
});
});

module("once method", {
setup : function(){
this.oClass = new TestClass({
"foo": 1,
"bar": 2
});
}
});

test("once method should be fire event one time.",function(){
//Given
var callCount = 0;
//When
this.oClass.once("test",function(){
callCount++;
});
this.oClass.trigger("test");
//Then
ok( callCount, 1);

//Given
//When
this.oClass.trigger("test");
//Then
ok( callCount, 1);
});

test("should be support object type.",function(){
//Given
var callCount = 0, callCount2 = 0;
//When
this.oClass.once({
"test": function(){
callCount++;
},
"test2": function(){
callCount2++;
}
});
this.oClass.trigger("test");
this.oClass.trigger("test2");
//Then
ok( callCount, 1);
ok( callCount2, 1);

//Given
//When
this.oClass.trigger("test");
this.oClass.trigger("test2");
//Then
ok( callCount, 1);
ok( callCount2, 1);
});

test("should be recevied parameters",function(){
//Given
var callCount = 0, e, a = {"a":1}, b = {"b":1}, param1;
this.oClass.once("test",function(event, aa){
callCount++;
e = event;
param1 = aa;
});

//When
this.oClass.trigger("test", a, b);

//Then
equal(a.a, e.a);
ok(typeof e.stop === "function");
equal(b, param1);
});

0 comments on commit 62f456e

Please sign in to comment.