Skip to content

Commit

Permalink
feat: finalize.callback called with result
Browse files Browse the repository at this point in the history
As `finalize` is called with `triggerHandler` we can read back the result from the handlers and forward to the `finalize` callback. This means we can now do this:

```javascript
var Widget = require("mu-jquery-widget/widget");
var W = Widget.extend({
  "on/finalize": function ($event, finalized) {
    finalized(function(result) {
      console.log(result === "result" // true
    });
    return "result";
  }
 });

var w = new W($("<div>", "ns");
w.remove();
```

BREAKING CHANGE: The removal of event handlers in `finalize` is removed in this commit as it's just as easy to do `w.off()` on your own when needed.
  • Loading branch information
mikaelkaron committed Mar 22, 2017
1 parent 647314b commit 05a5db9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 47 deletions.
45 changes: 11 additions & 34 deletions tests/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,54 +40,31 @@
assert.deepEqual(C.concat(), Widget.concat(a, b));
});

QUnit.test("finalize removes handlers sync", function (assert) {
QUnit.test("finalize triggered on .remove()", function (assert) {
assert.expect(1);

var count = 0;
var $element = $("<div></div>");
var W = Widget.extend({
"on/test": function () {
assert.ok(count++ === 0, "handler called " + count + " times");
"on/finalize": function () {
assert.ok(true, "handler called");
}
});
var w = new W($element, "ns");

$element
.trigger("test")
.trigger("finalize.ns")
.trigger("test");
$element.remove();
});

QUnit.test("finalize removes handlers async", function (assert) {
QUnit.test("finalize called with $.Callbacks", function (assert) {
assert.expect(2);

var count = 0;
var $element = $("<div></div>");
var W = Widget.extend({
"on/test": function () {
assert.ok(count++ <= 1, "handler called " + count + " times");
}
});
var w = new W($element, "ns");
var c = $.Callbacks("once");

$element
.trigger("test")
.trigger("finalize.ns", c.add)
.trigger("test");

c.fire();

$element.trigger("test");
});

QUnit.test("finalize triggered on .remove()", function (assert) {
assert.expect(1);

var $element = $("<div></div>");
var W = Widget.extend({
"on/finalize": function () {
assert.ok(true, "handler called");
"on/finalize": function ($event, finalized) {
finalized(function(result) {
assert.ok(true, "handler called");
assert.equal("result", result, "result matches");
});
return "result";
}
});
var w = new W($element, "ns");
Expand Down
14 changes: 1 addition & 13 deletions widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@
var me = this;
var $element = me.$element;
var finalized = $element.constructor.Callbacks("once");
$element.triggerHandler("finalize." + me.ns, finalized.add);
finalized.fire();
},
"on/finalize": function ($event, cb) {
var me = this;
if (cb) {
cb(function () {
me.off();
});
}
else {
me.off();
}
finalized.fire($element.triggerHandler("finalize." + me.ns, finalized.add));
}
};

Expand Down

0 comments on commit 05a5db9

Please sign in to comment.