Created
October 16, 2014 18:53
-
-
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
This file contains 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
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(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo on line 41:
})(_ps;
should be
})(_ps);