Created
December 1, 2012 20:24
-
-
Save nathanboktae/4184763 to your computer and use it in GitHub Desktop.
Use ko.subscribable in place of js-signals when using crossroads.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(factory, global) { | |
if (global.define && global.define.amd) { | |
global.define(['ko'], factory); | |
} else { | |
global.signals = factory(global.ko); | |
} | |
})(function(ko) { | |
return { | |
Signal: function() { | |
var sub = new ko.subscribable(); | |
this.add = sub.subscribe.bind(sub); | |
this.getNumListeners = sub.getSubscriptionsCount.bind(sub); | |
this.dispatch = function() { | |
var args = arguments; | |
if (sub._subscriptions.change) { | |
sub._subscriptions.change.slice(0).forEach(function (subscription) { | |
// In case a subscription was disposed during the arrayForEach cycle, check | |
// for isDisposed on each subscription before invoking its callback | |
if (subscription && (subscription.isDisposed !== true)) | |
subscription.callback.apply(subscription, args); | |
}); | |
} | |
}; | |
this.removeAll = this.dispose = function() { | |
var changeSubs = sub._subscriptions.change || []; | |
changeSubs.forEach(function(subscription) { | |
subscription.dispose(); | |
}); | |
sub._subscriptions.change = []; | |
}; | |
this.addOnce = function(callback) { | |
var subscription = sub.subscribe(function() { | |
callback.apply(sub, arguments); | |
subscription.dispose(); | |
}); | |
}; | |
} | |
}; | |
}, this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment