1 // ========================================================================== 2 // Project: SproutCore Costello - Property Observing Library 3 // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 // Portions ©2008-2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 8 /** 9 @class 10 */ 11 SC.Function = /** @scope SC.Function.prototype */{ 12 13 /** 14 @see Function.prototype.property 15 */ 16 property: function (fn, keys) { 17 fn.dependentKeys = SC.$A(keys); 18 var guid = SC.guidFor(fn); 19 fn.cacheKey = "__cache__" + guid; 20 fn.lastSetValueKey = "__lastValue__" + guid; 21 fn.isProperty = true; 22 return fn; 23 }, 24 25 /** 26 @see Function.prototype.cacheable 27 */ 28 cacheable: function (fn, aFlag) { 29 fn.isProperty = true; // also make a property just in case 30 if (!fn.dependentKeys) fn.dependentKeys = []; 31 fn.isCacheable = (aFlag === undefined) ? true : aFlag; 32 return fn; 33 }, 34 35 /** 36 @see Function.prototype.idempotent 37 */ 38 idempotent: function (fn, aFlag) { 39 fn.isProperty = true; // also make a property just in case 40 if (!fn.dependentKeys) this.dependentKeys = []; 41 fn.isVolatile = (aFlag === undefined) ? true : aFlag; 42 return fn; 43 }, 44 45 /** 46 @see Function.prototype.enhance 47 */ 48 enhance: function (fn) { 49 fn.isEnhancement = true; 50 return fn; 51 }, 52 53 /** 54 @see Function.prototype.observes 55 */ 56 observes: function (fn, propertyPaths) { 57 // sort property paths into local paths (i.e just a property name) and 58 // full paths (i.e. those with a . or * in them) 59 var loc = propertyPaths.length, local = null, paths = null; 60 while (--loc >= 0) { 61 var path = propertyPaths[loc]; 62 // local 63 if ((path.indexOf('.') < 0) && (path.indexOf('*') < 0)) { 64 if (!local) local = fn.localPropertyPaths = []; 65 local.push(path); 66 67 // regular 68 } else { 69 if (!paths) paths = fn.propertyPaths = []; 70 paths.push(path); 71 } 72 } 73 return fn; 74 } 75 76 }; 77