/*-------------------------------------------------------------------------- * * 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. * *--------------------------------------------------------------------------*/ /* Ideas for future stop using ids in collections, use full key instead so then cache can be like : { allProducts: ['product__1','product__2'], product__1: { name: 'red toy' }, product__2: { name: 'blue toy' } productCol: Emberjac.modelCollection( 'allProducts', // idCollection property, potentially path eg. 'App.cache.allProducts'. Binds to this and its contents function(aArray){ // optionally filter and/or sort array here, return result leaving original unmodified } ) } */ // 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.ObjectFactory.extend({ defaultClass: Ember.Object, createInstance: function(aClass,aProperties) { aProperties = aProperties || {}; return aClass.create(aProperties); } }); Kojac.EmberModel = Ember.Object.extend({ // set: function(k,v) { // var def = this.constructor.getDefinitions(); // var t = (def && def[k]); // if (t) // v = Kojac.interpretValueAsType(v,t); // return this._super(k,v); // }, // // setProperties: function(values) { // values = Kojac.readTypedProperties({},values,this.constructor.getDefinitions()); // return this._super(values); // }, // copy the property from source to dest // this could be a static fn toJsonoCopyFn: function(aDest,aSource,aProperty,aOptions) { aDest[aProperty] = Kojac.Utils.toJsono(Ember.get(aSource,aProperty),aOptions); }, // return array of names, or an object and all keys will be used // this could be a static fn toPropListFn: function(aSource,aOptions) { var p; if (p = aSource && aSource.constructor && aSource.constructor.proto && aSource.constructor.proto()) { if ((p = Ember.meta(p)) && (p = p.descs)) { var result = []; var m; var d; var k; var keys = _.keys(p); for (var i=0;i=0) { // pValue is field type destType = pValue; defaultValue = null; } else { var ft=Kojac.getPropertyValueType(pValue); if (ft && (Kojac.SimpleTypes.indexOf(ft)>=0)) { // pValue is simple field value destType = ft; defaultValue = pValue; } } if (destType) { extender[p] = Ember.computed(function(aKey,aValue){ // MyClass.metaForProperty('person'); var m = Ember.meta(this,false); var d = m && m.descs[aKey]; var v; if (arguments.length==2) { // set var t = d && d._meta && d._meta.type; if (t) v = Kojac.interpretValueAsType(aValue,t); else v = aValue; //cache[aKey] = v; } else { // get var cache = m.cache; v = Ember.cacheFor(this,aKey); if (cache && aKey in cache) { return cache[aKey]; } else { return d && d._meta && d._meta.value; } } return v; }).meta({ kemp: true, // Kojac Ember Model Property type: destType, value: defaultValue }) } else { extender[p] = pValue; } } } var result = this._super(extender); 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; }, retrieve: function(k) { return this.get(k); }, store: function(k,v) { this.beginPropertyChanges(); if (v===undefined) { this.set(k,v); delete this[k]; } else { this.set(k,v); } this.endPropertyChanges(); }, collectIds: function(aPrefix, aIds, aFilterFn) { if (!aIds) aIds = this.get(aPrefix); return Kojac.collectIds(aPrefix,aIds,this,aFilterFn); } }); Kojac.collectIds = function(aPrefix,aIds,aCache,aFilterFn) { if (!aIds) return []; var result = []; var item; for (var i=0;i