Skip to content

Instantly share code, notes, and snippets.

@rafaeljesus
Last active December 31, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeljesus/7931445 to your computer and use it in GitHub Desktop.
Save rafaeljesus/7931445 to your computer and use it in GitHub Desktop.
Module Pattern JS
var UserPurchases = (function() {
function UserPurchases(Q, UserSession, PurchaseModals) {
this.Q = Q;
this.UserSession = UserSession;
this.PurchaseModals = PurchaseModals;
}
UserPurchases.prototype.purchase = function(video) {
this.video = video;
return this.UserSession.signIn().then(this._promisePurchase(video));
};
UserPurchases.prototype.completePurchase = function(license) {
this.user.addLicense(this.video, license);
return this._purchaseDeferred.resolve(license);
};
UserPurchases.prototype._promisePurchase = function(video) {
var _this = this;
return function(user) {
_this.user = user;
_this._purchaseDeferred = _this.Q.defer();
_this._resolveOrPrompt(video);
return _this._purchaseDeferred.promise;
};
};
UserPurchases.prototype._resolveOrPrompt = function(video) {
if (this.user.owns(video)) {
return this.completePurchase(this.user.licenseFor(video));
} else {
return this.PurchaseModals.openSaleForm(video);
}
};
return UserPurchases;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment