Skip to content

Commit

Permalink
event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
purge committed Nov 27, 2013
1 parent 4709546 commit 516164a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
define(['require', 'lib/bonzo/bonzo', 'lib/qwery/qwery', 'lib/bean/bean', 'domReady!'], function(require, bonzo, qwery, bean) {

function SqwidgetCore() {}
function SqwidgetCore() {
this.eventBus = this.curryBean();
}

SqwidgetCore.prototype.packages = {};

Expand Down Expand Up @@ -57,6 +59,22 @@ define(['require', 'lib/bonzo/bonzo', 'lib/qwery/qwery', 'lib/bean/bean', 'domRe
return this.packages[opts.id] = opts;
};


//provide a curried bean.
SqwidgetCore.prototype.curryBean = function() {
var _this = this,
bus = {},
proxy = ['fire', 'on', 'off', 'once'];

for (var i = 0; i < proxy.length; i++) {
var n = proxy[i];
(function(n, _this) {
bus[n] = function(event, cb) { return bean[n](_this, event, cb); };
}(n, _this));
}
return bus;
};

//when the promise is resolved initialise the bundle controller
SqwidgetCore.prototype.initialiseWidget = function(pkg, bundle) {

Expand Down
4 changes: 2 additions & 2 deletions test/spec/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ define(['chai', 'lib/async/lib/async', 'core', 'lib/bonzo/bonzo', 'lib/bean/bean
async.parallel([

function(cb) {
bean.on(sqwidgetCore, 'rendered.' + widget.id, function(bundle) {
sqwidgetCore.eventBus.on('rendered.' + widget.id, function(bundle) {
assert.ok("Triggered event");
assert.equal(bonzo(w1).html(), '<div>TEST</div>', 'Rendered correctly');
assert.isTrue(bundle.require("dep1/helper"));
Expand All @@ -76,7 +76,7 @@ define(['chai', 'lib/async/lib/async', 'core', 'lib/bonzo/bonzo', 'lib/bean/bean
},

function(cb) {
bean.on(sqwidgetCore, 'rendered.' + w2r.id, function(bundle) {
sqwidgetCore.eventBus.on('rendered.' + w2r.id, function(bundle) {
assert.isTrue(bundle.require("dep2/helper"));
assert.isTrue(bundle.require("dep2/helper2"));
assert.ok("Triggered event");
Expand Down

0 comments on commit 516164a

Please sign in to comment.