Last active
December 31, 2015 03:59
-
-
Save rafaeljesus/7931445 to your computer and use it in GitHub Desktop.
Module Pattern JS
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
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