Created
November 28, 2011 16:00
-
-
Save lamp/1400887 to your computer and use it in GitHub Desktop.
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
| if(_.isUndefined(providers.Def)) providers.Def = {}; | |
| providers.Def.Product = function(attrs){ | |
| this.init(attrs); | |
| }; | |
| providers.Def.Product.prototype = { | |
| init : function(attrs){ | |
| }, | |
| sku : function(){ | |
| }, | |
| title: function(){ | |
| }, | |
| image_uri : function(){ | |
| }, | |
| description : function(){ | |
| }, | |
| render : function(){ | |
| } | |
| }; | |
| providers.Def.Product.lookup = function(callback){ | |
| var parse = function(data){ | |
| try{ | |
| return JSON.parse(data); | |
| }catch(e){ | |
| player.log('JSON parse failed ' + e); | |
| } | |
| }; | |
| var stem = (config.simple_mode) ? player.get('simpleModeDataURL') : player.get('productDataURL'); | |
| var request_name = ['productData', player.presentation.seedName, player.presentation.id].join(':'); | |
| net.globalCache(requestName, 5 * 60); | |
| net.http.get([stem, player.presentation.id + '.json'].join('/'), {}, function(response){ | |
| if(response.status != 200){ | |
| net.http.get(player.get('simpleModeDataURL') + '/' + player.presentation.id + '.json', {}, function(response){ | |
| if(response.status != 200){ | |
| player.log('Failed to find product data'); | |
| return; | |
| } | |
| callback(parse(response.body)); | |
| }); | |
| }else{ | |
| callback(parse(response.body)); | |
| } | |
| }, request_name); | |
| }; | |
| providers.Def.Basket = function(){}; | |
| providers.Def.Basket.prototype = { | |
| init : function(){ | |
| this.cache = new PlayerCache(); | |
| this.create(); | |
| }, | |
| create : function(){ | |
| var scope = this; | |
| Product.lookup(function(data){ | |
| if(typeof data.products != 'object') { | |
| player.log('no products found'); | |
| return; | |
| } | |
| scope.products = scope.provider.coerce(data.products); | |
| scope.provider.checkout_url(data.basketURL); | |
| if(!_.isUndefined(data.uiStringsByLanguage)) localizer.update(data.uiStringsByLanguage); | |
| scope.trigger('ready'); | |
| }); | |
| }, | |
| add : function(sku) { | |
| if(this.has(sku)) return; | |
| this.provider.validateSKU(sku); | |
| var skus = this.skus(); | |
| skus.push(sku); | |
| this.cache.set('basketSKUs', skus); | |
| this.trigger('updated'); | |
| }, | |
| checkout : function() { | |
| return player.get('basketURL') + '?skus=' + this.products().join('|'); | |
| }, | |
| find : function(sku) { | |
| this.provider.validateSKU(sku); | |
| return _.first(_.select(this.products, function(product){ | |
| return product.sku == sku; | |
| })); | |
| }, | |
| products : function(){ | |
| if(_.isUndefined(this.products)) this.products = this.cache.get('products'); | |
| return this.products; | |
| }, | |
| has : function(sku){ | |
| _.any(this.skus(), function(skued){ | |
| return sku === skued | |
| }); | |
| }, | |
| remove : function(sku) { | |
| this.provider.validateSKU(sku); | |
| var skus = this.skus(), filteredSKUs = [] | |
| for(var i in skus) { | |
| var s = skus[i] | |
| if(s != sku) filteredSKUs.push(s) | |
| } | |
| this.cache.set('basketSKUs', filteredSKUs) | |
| this.trigger('updated') | |
| }, | |
| skus : function(){ | |
| this.cache.get('basketSKUs', function() { return [] }); | |
| }, | |
| validateCallback : function(sku) { | |
| if(typeof(sku) != 'function') throw 'expected callback to be a function' | |
| }, | |
| validateSKU : function (sku) { | |
| if(typeof(sku) != 'string' || sku.length == 0) throw 'expected SKU to be a non-empty string' | |
| }, | |
| checkout_url : function(url){ | |
| player.set('basketURL', data.basketURL); | |
| }, | |
| coerce : function(products, response){ | |
| return _.map(products, function(product, key){ | |
| var ident = 'product ' + (1 * i + 1) + ' from ' + response.url | |
| player.log(JSON.stringify(products)); | |
| if(config.basket_mode) { | |
| if(typeof(product.sku) != 'string') { | |
| player.log(ident + ' does not contain a valid sku but basket mode is enabled: aborting') | |
| return | |
| } | |
| return new Product({}); | |
| } else { | |
| if(typeof(product.linkURL) != 'string') { | |
| player.log(ident + ' does not contain a valid linkURL but basket mode is disabled: aborting') | |
| return; | |
| } | |
| } | |
| }); | |
| } | |
| }; | |
| player.log(JSON.stringify(providers.Def)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment