Created
June 7, 2013 23:55
-
-
Save jpdevries/5733212 to your computer and use it in GitHub Desktop.
jQuery Class Example. Originally written for Holden Outerwear 2011 site.
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
/* | |
upcModel used to get upc when provided fabric, colorway, and size | |
invModel used to get the regions and retailers carring the given upc | |
*/ | |
/* __ | |
/\ \ /\ \__ | |
\_\ \ __ \ \ ,_\ __ | |
/'_` \ /'__`\ \ \ \/ /'__`\ | |
/\ \L\ \/\ \L\.\_\ \ \_/\ \L\.\_ | |
\ \___,_\ \__/.\_\\ \__\ \__/.\_\ | |
\/__,_ /\/__/\/_/ \/__/\/__/\/*/ | |
/* model will be fed a fabric, colorway, and a size, and return it's associated UPC code | |
to be used with P2P dynamic inventory across global retailers | |
this will be handy for the ajax when users change colorways and sizes */ | |
/*var upcModel = [ | |
// every fabric | |
new FabricDTO('11220', | |
[ | |
// holds one or more colorways | |
new ColorwayDTO( 'flint-black', [ | |
// which hold one or more UPCs | |
new UPCDTO('693373041956', 'xs') | |
] ) | |
] | |
) | |
//, more new FabricDTO()... | |
];*/ | |
/* haven't decided if this will be intially rendered in the markup, or if it will be loaded with ajax, | |
but either way... | |
*/ | |
/* a cron job will generate a file for each product contains this JSON object | |
ex: /api/p2p/upc-retailers/13.js | |
CRON JOB: | |
- twice a day, hits upc-xml.xml file | |
- for every product, creates an invModel data object and saves to file | |
- the file will eseentially be created by loading the existing upc xml files associated with that product and creating a new UPC object as seen below | |
NOTE: It may seem odd seperating the "get upc" data above from the "get retailers" logic from below but it makes sense because: | |
- the "get retailers" data relies on a cron job and third party feeds, it may fail so i think it makes sense to not combine reliable and unreliable data | |
- rendering out the JSON for the products as individual files on the server makes for a more reusable API | |
*/ | |
/*var invModel = [ | |
new UPC('693373041956', [ | |
new Region('us', [ | |
new Retailer('exitrealworld', "http://upc.tactics.com/holden/10203040") | |
]) | |
]) | |
];*/ | |
/* ___ | |
/\ \ /\_ \ | |
___ ___ ___ \_\ \ __\//\ \ | |
/' __` __`\ / __`\ /'_` \ /'__`\\ \ \ | |
/\ \/\ \/\ \/\ \L\ \/\ \L\ \/\ __/ \_\ \_ | |
\ \_\ \_\ \_\ \____/\ \___,_\ \____\/\____\ | |
\/_/\/_/\/_/\/___/ \/__,_ /\/____/\/___*/ | |
/* provided the current fabric, colorway,and size | |
returns the associated UPC barcode | |
NOTE: rewritten to be more verbose using custom class methods */ | |
function getUPC(_alias, _colorway, _size) { | |
var l = upcModel.length; | |
for(var i = 0; i < l; i++) { | |
var _f = upcModel[i]; | |
// fabric found, search for the UPCDTO | |
if(_f.alias == _alias) try { return _f.getColorway(_colorway).getUPC(_size).upc || null } catch(e) {}; | |
} | |
// nothing found | |
return null; | |
} | |
function getFabric(_fabric) { | |
var l = upcModel.length; | |
for(var i = 0; i < l; i++) { | |
var _f = upcModel[i]; | |
// fabric found, search for the UPCDTO | |
if(_f.alias == _fabric) return _f; | |
} | |
// nothing found | |
return null; | |
} | |
function getUPCDTO(_upc) { | |
var l = upcModel.length; | |
for(var i = 0; i < l; i++) { | |
var _f = upcModel[i]; | |
var _pc = _f.getUPC(_upc) || null; | |
if(_pc) return _pc; | |
} | |
// nothing found | |
return null; | |
} | |
/* provided a upc code, returns the JSON object containing the p2p retailer inventory for that item */ | |
function getUPCInv(_upc) { | |
var l = invModel.length; | |
//console.log('getUPCInv: ' + l); | |
for(var i = 0; i < l; i++) { | |
var _u = invModel[i]; | |
//console.log(_u.upc); | |
if(_u.upc == _upc) return _u; | |
} | |
return null; | |
} | |
function getUPCPrice(_upc) { | |
var l = priceData.length; | |
for(var i = 0; i < l; i++) { | |
var _upcPrice = priceData[i]; | |
if(_upcPrice.upc == _upc) return _upcPrice.price; | |
} | |
return null; | |
} | |
/*_ | |
/\_ \ | |
___\//\ \ __ ____ ____ __ ____ | |
/'___\\ \ \ /'__`\ /',__\ /',__\ /'__`\ /',__\ | |
/\ \__/ \_\ \_/\ \L\.\_/\__, `\/\__, `\/\ __//\__, `\ | |
\ \____\/\____\ \__/.\_\/\____/\/\____/\ \____\/\____/ | |
\/____/\/____/\/__/\/_/\/___/ \/___/ \/____/\/__*/ | |
// FabricDTO class, holds a style number and one or more colorways | |
var FabricDTO = function(_alias, _colorways) { | |
this.alias = _alias; | |
this.colorways = _colorways; | |
} | |
$.extend(FabricDTO.prototype,{ | |
// provided a colorway name, returns the associated ColorwayDTO | |
getColorway:function(_colorway) { | |
var _colorways = this.colorways; | |
var l = _colorways.length; | |
for(var i = 0; i < l; i++) { | |
var _c = _colorways[i]; | |
if(_c.name == _colorway) return _c; | |
} | |
return null; | |
}, | |
getUPC:function(_upc) { | |
var _colorways = this.colorways; | |
var l = _colorways.length; | |
for(var i = 0; i < l; i++) { | |
var _c = _colorways[i]; | |
var _pc = _c.getUPCByUPC(_upc) || null; | |
if(_pc) return _pc; | |
} | |
return null; | |
}, | |
toString:function() { | |
return "alias: " + this.alias + " colorways: " + this.colorways; | |
} | |
}); | |
// ColorwayDTO class, holds a name and one or more UPCDTO objects | |
var ColorwayDTO = function(_name, _upcs) { | |
this.name = _name; | |
this.upcs = _upcs; | |
} | |
$.extend(ColorwayDTO.prototype,{ | |
// provided a size, returns the associated UPC barcode | |
getUPC:function(_size) { | |
var _upcs = this.upcs; | |
var l = _upcs.length; | |
for(var i = 0; i < l; i++) { | |
var _u = _upcs[i]; | |
if(_u.size == _size) return _u; | |
} | |
return null; | |
}, | |
getUPCByUPC:function(_upc) { | |
var _upcs = this.upcs; | |
var l = _upcs.length; | |
for(var i = 0; i < l; i++) { | |
var _u = _upcs[i]; | |
if(_u.upc == _upc) return _u; | |
} | |
return null; | |
}, | |
toString:function() { | |
return "name: " + this.name + " upcs: " + this.upcs; | |
} | |
}); | |
// UPCDTO class, holds a upc code and a size | |
var UPCDTO = function(_upc, _shopId, _size) { | |
this.upc = _upc; | |
this.shopId = _shopId; | |
this.size = _size; | |
} | |
$.extend(UPCDTO.prototype,{ | |
toString:function() { | |
return "upc: " + this.upc + " shopId: " + this.shopId + " size: " + this.size; | |
} | |
}); | |
// UPCDTO class, holds a upc code and a size | |
var UPCInv = function(_upc, _regions) { | |
this.upc = _upc; | |
this.regions = _regions; | |
} | |
$.extend(UPCInv.prototype,{ | |
toString:function() { | |
var _s = "\n\nupc: " + this.upc + "\n"; | |
var l = this.regions.length; | |
for(var i = 0; i < l; i++) { | |
var _r = this.regions[i]; | |
_s += "\nregion: " + _r.alias; | |
var k = _r.retailers.length; | |
for(var j = 0; j < k; j++) { | |
var _ret = _r.retailers[i]; | |
if(_ret) _s += "\nretailer: " + _ret.id + " " + _ret.link; | |
} | |
} | |
return _s; | |
}, | |
getRetailers:function() { | |
var _stores = new Array(); | |
var l = this.regions.length; | |
for(var i = 0; i < l; i++) { | |
var _region = this.regions[i]; | |
var k = _region.retailers.length; | |
for(var j = 0; j < k; j++) { | |
_stores.push(_region.retailers[j]); | |
} | |
} | |
return _stores; | |
}, | |
getRetailersClasses:function(_prefix, _storePrefix) { | |
var _a = new Array(); | |
var _stores = this.getRetailers(); | |
var l = _stores.length; | |
for(var i = 0; i < l; i++) { | |
_a.push(_prefix + _stores[i].id); | |
} | |
return _a.join(','); | |
} | |
}); | |
// UPCDTO class, holds a upc code and a size | |
var Region = function(_alias, _retailers) { | |
this.alias = _alias; | |
this.retailers = _retailers; | |
} | |
$.extend(Region.prototype,{ | |
toString:function() { | |
return ""; | |
//return "upc: " + this.upc + " size: " + this.size; | |
} | |
}); | |
// UPCDTO class, holds a upc code and a size | |
var Retailer = function(_id, _link) { | |
this.id = _id; | |
this.link = _link; | |
} | |
$.extend(Retailer.prototype,{ | |
toString:function() { | |
return ""; | |
//return "upc: " + this.upc + " size: " + this.size; | |
} | |
}); | |
// UPCPrice class, holds a upc code and a price | |
var UPCPrice = function(_upc, _price) { | |
this.upc = _upc; | |
this.price = _price; | |
} | |
$.extend(UPCPrice.prototype,{ | |
toString:function() { | |
return ""; | |
//return "upc: " + this.upc + " size: " + this.size; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment