Skip to content

Commit

Permalink
fix(widget): subscribe on initialize
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Previous to this change widgets would subscribe to the
hub in the constructor and if the hub implemented callback-on-subscribe
those handlers would execute before the initialize event triggered on the
widget. This change fixes that as subscriptionsare now done during
`initialize`.
  • Loading branch information
mikaelkaron committed Sep 25, 2016
1 parent a427ab8 commit 241bf20
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
40 changes: 20 additions & 20 deletions dist/all.js
Expand Up @@ -632,26 +632,6 @@
}
});

(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-jquery-app/create"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-jquery-app")];
}));
}
})([
"mu-create/create",
"mu-create/constructor",
"mu-create/prototype",
"mu-jquery-widget/dom",
"./hub"
], this, function(create, construct, proto, dom, hub) {
return create(construct, hub, dom, proto);
});

(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define(modules, factory);
Expand All @@ -672,3 +652,23 @@
return false;
});
});

(function(modules, root, factory) {
if (typeof define === "function" && define.amd) {
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-jquery-app/create"] = factory.apply(root, modules.map(function(m) {
return root[m.replace(/^\./, "mu-jquery-app")];
}));
}
})([
"mu-create/create",
"mu-create/constructor",
"mu-create/prototype",
"mu-jquery-widget/dom",
"./hub"
], this, function(create, construct, proto, dom, hub) {
return create(construct, hub, dom, proto);
});
1 change: 0 additions & 1 deletion dist/all.min.js

This file was deleted.

5 changes: 5 additions & 0 deletions examples/app.js
Expand Up @@ -26,6 +26,11 @@
return weave.apply(this.find("[mu-widget]"), ["mu-widget", load, hub].concat(slice.call(arguments)));
};

/// define $.fn.crank` with attribut set and args set
jQuery.fn.crank = function() {
return crank.apply(this.find("[mu-widget]"), ["mu-widget"].concat(slice.call(arguments)));
};

// wait for `ready`
jQuery(function ($) {
// publish initial values on the `test` topic
Expand Down
2 changes: 1 addition & 1 deletion examples/button.js
Expand Up @@ -21,7 +21,7 @@

return $.Deferred(function(deferred) {
console.log("initialize %o", $event);
setTimeout(deferred.resolve, 1000);
setTimeout(deferred.resolve, 100);
})
.done(function() {
console.log("initialized %o", $event);
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js
Expand Up @@ -18,8 +18,8 @@ gulp.task("default", function() {
"bower_components/mu-jquery-widget/widget.js",
"bower_components/mu-jquery-widget/dom.js",
"bower_components/mu-jquery-hub/hub.js",
"./create.js",
"./hub.js"
"./hub.js",
"./create.js"
])
.pipe(concat("all.js"))
.pipe(gulp.dest("./dist/"));
Expand Down
28 changes: 16 additions & 12 deletions widget.js
@@ -1,16 +1,16 @@
(function(modules, root, factory) {
(function (modules, root, factory) {
if (typeof define === "function" && define.amd) {
define(modules, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory.apply(root, modules.map(require));
} else {
root["mu-jquery-app/widget"] = factory.apply(root, modules.map(function(m) {
root["mu-jquery-app/widget"] = factory.apply(root, modules.map(function (m) {
return {
"jquery": root.jQuery
}[m] || root[m.replace(/^\./, "mu-jquery-app")];
}));
}
})([ "jquery", "mu-jquery-widget/widget" ], this, function($, widget) {
})(["jquery", "mu-jquery-widget/widget"], this, function ($, widget) {
var slice = Array.prototype.slice;
var concat = Array.prototype.concat;

Expand All @@ -33,30 +33,34 @@
};

return concat.call(widget,
function($element, ns, hub) {
function ($element, ns, hub) {
var me = this;

me.subscribe = function(topic, handler) {
me.subscribe = function (topic, handler) {
return hub(topic).subscribe.call(this, handler);
};

me.unsubscribe = function(topic, handler) {
me.unsubscribe = function (topic, handler) {
return hub(topic).unsubscribe.call(this, handler);
};

me.publish = function(topic) {
me.publish = function (topic) {
var t = hub(topic);
var p = t.publish;

return p.apply(this, slice.call(arguments, 1));
};

$.each(me.constructor.hub || false, function(index, op) {
me.subscribe(op.topic, op.handler);
});
},
{
"on/_remove": function() {
"on/initialize": function() {
var me = this;

$.each(me.constructor.hub || false, function (index, op) {
me.subscribe(op.topic, op.handler);
});
},

"on/_remove": function () {
this.$element.triggerHandler("finalize." + this.ns);
}
});
Expand Down

0 comments on commit 241bf20

Please sign in to comment.