/*-------------------------------------------------------------------------- * * Key Oriented JSON Application Cache (KOJAC) * (c) 2011-12 Buzzware Solutions * https://github.com/buzzware/KOJAC * * KOJAC is freely distributable under the terms of an MIT-style license. * *--------------------------------------------------------------------------*/ // recommended http://www.cerebris.com/blog/2012/03/06/understanding-ember-object/ //var Person = Ember.Object.extend({ // chromosomes: null, // init: function() { // this._super(); // this.chromosomes = ["x"]; // everyone gets at least one X chromosome // } //}); // declaring property : {chromosomes: null} // for values, generate function that calls this._super(); then loops thru setting values // example : // //Product = Kojac.EmberModel.extend({ // name: String, // purchases: Int, // weight: Number, // isMember: Boolean, // init: function() { // this._super(); // this.weight = 0.0; // } //}); // // // 1. Create extendObject with properties: null // 2. Kojac.EmberObjectFactory = Kojac.Object.extend({ matchers: null, register: function(aPairs) { if (!aPairs) return; if (this.matchers===null) this.matchers = []; for (var i = 0; i < aPairs.length; i++) this.matchers.push(aPairs[i]); }, emberClassFromKey: function(aKey) { var pair; var re; var newClass; for (var i = 0; i < this.matchers.length; i++) { pair = this.matchers[i]; re = pair[0]; if (!re.test(aKey)) continue; newClass = pair[1]; break; } if (newClass===undefined) newClass = Ember.Object; return newClass; }, emberObjectFactoryArray: function(aArray,aKey) { var newClass = this.emberClassFromKey(aKey); var result = []; for (var i=0; i=0) { // pValue is field type definitions[p] = pValue; defaults[p] = null; extender[p] = null; } else { var ft=Kojac.getPropertyValueType(pValue); if (ft && (Kojac.SimpleTypes.indexOf(ft)>=0)) { // pValue is simple field value definitions[p] = ft; defaults[p] = pValue; extender[p] = pValue; } else { // pValue is something else //definitions[p] = _type; //defaults[p] = _value; extender[p] = pValue; } } // } } // extender.init = function() { // we need to call the super init, then initialise default values, then call the given init // this._super(); // //var defaults = this.constructor.getDefaults(); // //for (dp in defaults) // // this[dp] = defaults[dp]; // if (_init) { // var _super = this._super; // this._super = function() {}; // temporarily disable _super to make init function work like normal ember init // _init.call(this); // if (_super) // this._super = _super; // restore _super // } // }; } var result = this._super(extender); result.setDefinitions(definitions); result.setDefaults(defaults); return result; }, setDefinitions: function(aDefinitions) { this._definitions = (aDefinitions || {}); }, getDefinitions: function() { return this._definitions || {}; }, setDefaults: function(aDefaults) { this._defaults = (aDefaults || {}); }, getDefaults: function() { return this._defaults || {}; }, __createWithMixins: Kojac.EmberModel.createWithMixins, createWithMixins: function() { var inputs = arguments; if (inputs.length) { inputs[0] = Kojac.readTypedProperties({},inputs[0],this.getDefinitions()); } return this.__createWithMixins.apply(this,inputs); }, __create: Kojac.EmberModel.create, create: function() { var inputs = arguments; if (inputs.length) { inputs[0] = Kojac.readTypedProperties({},inputs[0],this.getDefinitions()); } return this.__create.apply(this,inputs); } }); Kojac.EmberModel.reopen({ ___set: Ember.set, set: function(k,v) { var def = this.constructor.getDefinitions(); var t = (def && def[k]); if (t) v = Kojac.interpretValueAsType(v,t); return this.___set(this,k,v); }, ___setProperties: Ember.setProperties, setProperties: function(values) { values = Kojac.readTypedProperties({},values,this.constructor.getDefinitions()); return this.___setProperties(this,values); }, toObject: function(aOptions) { var result = {}; var defs = this.constructor.getDefinitions(); var includes = aOptions && aOptions.include for (var p in defs) result[p] = this.get(p); if (includes) { includes = this.getProperties(includes); var v; for (var p in includes) { v = includes[p]; if (v && (typeof(v)=="object") && ("toObject" in v)) includes[p] = v.toObject(); } _.extend(result,includes); } return result; } }); Kojac.EmberCache = Ember.Object.extend({ generateKey: function(aPrefix) { var key; do { key = aPrefix+'__'+(-_.random(1000000,2000000)).toString(); } while (key in this); return key; }, generateId: function(aPrefix) { var key; var id; do { id = -_.random(1000000,2000000) key = aPrefix+'__'+id.toString(); } while (key in this); return id; }, store: function(aKeysValues) { this.beginPropertyChanges(); for (p in aKeysValues) { if (aKeysValues[p]===undefined) { this.set(p,undefined); delete this[p]; } else { this.set(p,aKeysValues[p]); } } this.endPropertyChanges(); }, cacheResults: function(aRequest) { this.beginPropertyChanges(); for (var i=0;i