Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Created October 16, 2014 18:53
Show Gist options
  • Save netpoetica/f448d98383f53cd3e0e4 to your computer and use it in GitHub Desktop.
Save netpoetica/f448d98383f53cd3e0e4 to your computer and use it in GitHub Desktop.
Even more decoupled than the original - pass dependencies in as args so that they can be easily changed later
document.addEventListener("DOMContentLoaded", function(event) {
/*
Later you can easily change this to any other PubSub library you like.
var _pubSub = PubSub;
or you can even wrap it to define your own API. This way, later on
you can still continue to use _ps.sub and _ps.pub even if you change
out the library defining that behaviour. Let's go with the latter.
*/
var _ps = {
pub: PubSub.publish,
sub: PubSub.subscribe
}
var orderModule = (function(ps) {
var orders = {},
EST_DELIVERY = 'current estimated delivery time',
estimatedDeliveryTime;
ps.subscribe(EST_DELIVERY, function(msg, data) {
console.log(msg);
estimatedDeliveryTime = data;
});
return orders;
})(_ps);
var deliveryModule = (function(ps) {
var deliveries = {},
EST_DELIVERY = 'current estimated delivery time';
deliveries.getEstimatedDeliveryTime = function() {
var estimatedDeliveryTime = 1; // Hard-coded to 1 hour, but likely an API call.
ps.publish(EST_DELIVERY, estimatedDeliveryTime);
};
return deliveries;
})(_ps;
deliveryModule.getEstimatedDeliveryTime();
});
@dxnn
Copy link

dxnn commented Oct 17, 2014

Typo on line 41:
})(_ps;
should be
})(_ps);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment