Skip to content

Commit 1028a6d

Browse files
committed
feat(core base): Add one event listener to listen for Pattern events only once.
1 parent 3d91ddc commit 1028a6d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/core/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ Base.prototype = {
6161
on(eventName, eventCallback) {
6262
this.$el.on(`${eventName}.${this.name}.patterns`, eventCallback);
6363
},
64+
one(eventName, eventCallback) {
65+
this.$el.one(`${eventName}.${this.name}.patterns`, eventCallback);
66+
},
6467
emit(eventName, args) {
6568
// args should be a list
6669
if (args === undefined) {

src/core/base.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,33 @@ describe("pat-base: The Base class for patterns", function () {
135135
new Tmp3($("<div>"), { option: "value" });
136136
});
137137

138-
it("has on/emit helpers to prefix events", function () {
138+
it("has on/emit helpers to prefix events", function (done) {
139139
var Tmp = Base.extend({
140140
name: "tmp",
141141
trigger: ".pat-tmp",
142142
init: function () {
143143
this.on("something", function (e, arg1) {
144144
expect(arg1).toEqual("yaay!");
145+
done();
146+
});
147+
this.emit("somethingelse", ["yaay!"]);
148+
},
149+
});
150+
new Tmp(
151+
$("<div/>").on("somethingelse.tmp.patterns", function (e, arg1) {
152+
$(this).trigger("something.tmp.patterns", [arg1]);
153+
})
154+
);
155+
});
156+
157+
it("has ``one`` helper to prefix events", function (done) {
158+
var Tmp = Base.extend({
159+
name: "tmp",
160+
trigger: ".pat-tmp",
161+
init: function () {
162+
this.one("something", function (e, arg1) {
163+
expect(arg1).toEqual("yaay!");
164+
done();
145165
});
146166
this.emit("somethingelse", ["yaay!"]);
147167
},

0 commit comments

Comments
 (0)