UNPKG

suki.sprd

Version:

Spreadshirt extension for suki.js

242 lines (177 loc) 3.98 kB
define([ './suki.js', './polyfill.js' ], function(suki, polyfill) { suki = suki.extend(); // --- var Article, article; /** * TODO: check @params * TODO: check @example * TODO: check if every example has its test representation */ /** * @constructor */ Article = function(name, defaults, debug) { this.attributes = { _before : [], _config : { functionAutoExec : true, schemaStrict : false, idKey : '_id' }, _connection : null, _debug : false, _events : new suki.Events(), _init : [], _injected : {}, _injectedList : [], _name : name || null, _schema : null, // --- fullData : false, attributeSet : false, // --- id : null, name : null, // --- custom : { views : null, selections : null } }; // --- suki.util.extend(this.attributes, defaults || {}); // --- this.attributesPrev = {}; }; Article.prototype = new suki.Model; Article.prototype._prepareViews = function() { var defaultView = this.get('product.defaultValues.defaultView.id'); var views = this.get('product.productType.views'); var resources = this.get('resources'); var composition; var arr = [] , url , i; for (i = 0; i < resources.length; i++) { if (resources[i].type === 'product') { url = resources[i].href.split('/views')[0]; } if (resources[i].type === 'composition') { composition = resources[i].href; } } arr.push({ type : 'default', id : defaultView, href : url + '/views/' + defaultView }); arr.push({ id : '0', type : 'composition', href : composition }); for (i = 0; i < views.length; i++) { if (views[i].id !== defaultView) { arr.push({ id : views[i].id, type : 'view', name : views[i].name, href : url + '/views/' + views[i].id }); } } this.set('custom.views', arr); }; Article.prototype._prepareSelections = function() { this.set('custom.selections', { view : this.get('custom.views')[0], size : null //appearance : this.get('custom.appearances')[0] }); }; Article.prototype.connect = function(connection) { if (connection.me() === 'connection') { this.set('_connection', connection); } else { console.error('.connect() failed, instance not found'); } return this; }; Article.prototype.create = function() { }; Article.prototype.retrieve = function(options, fn) { var that = this , query = {} , conn = this.get('_connection'); // --- if (suki.util.is.function(options)) { fn = options; options = {}; } // --- query = { fullData : this.get('fullData'), attributeSet : this.get('attributeSet') }; suki.util.extend(query, options); // --- conn.api.get('/articles/' + this.get('id'), query, function(err, res) { if (err) return fn(err); that.set(res.body); that._prepareViews(); that._prepareSelections(); setTimeout(function() { if (fn) fn(null, res); }, 1000); }); }; Article.prototype.update = function() { }; Article.prototype.delete = function() { }; /** * @static * Static object that holds methods to be exported to `requirejs`. */ article = Article; /** * @function .init() * Creates new instance of `Article`, exports API. * @param (str)(req) name * @param (obj)(opt) defaults * @return {Object} * * @api get * @api set * @api end * * @static */ article.init = function(name, defaults, debug) { if (suki.util.is.boolean(defaults)) { debug = defaults; defaults = {}; } else { defaults = defaults || {}; } // --- var inst , exports; // --- inst = new Article(name, defaults, debug); // --- // TODO: export clean API exports = inst; // --- if (inst.get('_debug') === true) { suki.util.extend(exports, inst); } // --- return exports; }; return Article; });