observable.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // wrapped by build app
  2. define("dojox/lang/observable", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.observable");
  4. // Used to create a wrapper object with monitored reads and writes
  5. //
  6. dojo.experimental("dojox.lang.observable");
  7. // IMPORTANT DISCLAIMER:
  8. // This is experimental and based on hideous hacks.
  9. // There are severe limitations on the ability of wrapper objects:
  10. // Only properties that have vbscript-legal names are accessible (similar to JavaScript, but they can't start with an underscore).
  11. // The wrapper objects are not expando in IE, because they are built
  12. // from VBScript objects. This means you can't add new properties after an object is created.
  13. // The wrapper objects can not be used a prototype for other objects.
  14. // Only properties with primitive values can be wrapped.
  15. // This has performance implications as well.
  16. dojox.lang.observable = function(/*Object*/wrapped,/*function*/onRead,/*function*/onWrite,/*function*/onInvoke){
  17. // summary:
  18. // Creates a wrapper object, which can be observed. The wrapper object
  19. // is a proxy to the wrapped object. If you will be making multiple wrapper
  20. // objects with the same set of listeners, it is recommended that you
  21. // use makeObservable, as it is more memory efficient.
  22. //
  23. // wrapped:
  24. // The object to be wrapped and monitored for property access and modification
  25. //
  26. // onRead:
  27. // See dojox.lang.makeObservable.onRead
  28. // onWrite:
  29. // See dojox.lang.makeObservable.onWrite
  30. // onInvoke:
  31. // See dojox.lang.makeObservable.onInvoke
  32. return dojox.lang.makeObservable(onRead,onWrite,onInvoke)(wrapped);
  33. }
  34. dojox.lang.makeObservable = function(/*function*/onRead,/*function*/onWrite,/*function*/onInvoke,/*Object*/hiddenFunctions){
  35. // summary:
  36. // Creates and returns an observable creator function. All the objects that
  37. // are created with the returned constructor will use the provided onRead and
  38. // onWrite listeners.
  39. // The created constructor should be called with a single argument,
  40. // the object that will be wrapped to be observed. The constructor will
  41. // return the wrapper object.
  42. //
  43. // onRead:
  44. // This is called whenever one of the wrapper objects created
  45. // from the constructor has a property that is accessed. onRead
  46. // will be called with two arguments, the first being the wrapped object,
  47. // and the second is the name of property that is being accessed.
  48. // The value that onRead returns will be used as the value returned
  49. // by the property access
  50. //
  51. // onWrite:
  52. // This is called whenever one of the wrapper objects created
  53. // from the constructor has a property that is modified. onWrite
  54. // will be called with three arguments, the first being the wrapped object,
  55. // the second is the name of property that is being modified, and the
  56. // third is the value that is being set on the property.
  57. //
  58. // onInvoke:
  59. // This is called when a method on the object is invoked. The first
  60. // argument is the wrapper object, the second is the original wrapped object,
  61. // the third is the method name, and the fourth is the arguments.
  62. //
  63. // hiddenFunctions:
  64. // allows you to define functions that should be delegated
  65. // but may not be enumerable on the wrapped objects, so they must be
  66. // explicitly included
  67. //
  68. // example:
  69. // The following could be used to create a wrapper that would
  70. // prevent functions from being accessed on an object:
  71. // | function onRead(obj,prop){
  72. // | return typeof obj[prop] == 'function' ? null : obj[prop];
  73. // | }
  74. // | var observable = dojox.lang.makeObservable(onRead,onWrite);
  75. // | var obj = {foo:1,bar:function(){}};
  76. // | obj = observable(obj);
  77. // | obj.foo -> 1
  78. // | obj.bar -> null
  79. //
  80. hiddenFunctions = hiddenFunctions || {};
  81. onInvoke = onInvoke || function(scope,obj,method,args){
  82. // default implementation for onInvoke, just passes the call through
  83. return obj[method].apply(scope,args);
  84. };
  85. function makeInvoker(scope,wrapped,i){
  86. return function(){
  87. // this is function used for all methods in the wrapper object
  88. return onInvoke(scope,wrapped,i,arguments);
  89. };
  90. }
  91. if(dojox.lang.lettableWin){ // create the vb class
  92. var factory = dojox.lang.makeObservable;
  93. factory.inc = (factory.inc || 0) + 1;
  94. // create globals for the getters and setters so they can be accessed from the vbscript
  95. var getName = "gettable_"+factory.inc;
  96. dojox.lang.lettableWin[getName] = onRead;
  97. var setName = "settable_"+factory.inc;
  98. dojox.lang.lettableWin[setName] = onWrite;
  99. var cache = {};
  100. return function(wrapped){
  101. if(wrapped.__observable){ // if it already has an observable, use that
  102. return wrapped.__observable;
  103. }
  104. if(wrapped.data__){
  105. throw new Error("Can wrap an object that is already wrapped");
  106. }
  107. // create the class
  108. var props = [], i, l;
  109. for(i in hiddenFunctions){
  110. props.push(i);
  111. }
  112. var vbReservedWords = {type:1,event:1};
  113. // find the unique signature for the class so we can reuse it if possible
  114. for(i in wrapped){
  115. if(i.match(/^[a-zA-Z][\w\$_]*$/) && !(i in hiddenFunctions) && !(i in vbReservedWords)){ //can only do properties with valid vb names/tokens and primitive values
  116. props.push(i);
  117. }
  118. }
  119. var signature = props.join(",");
  120. var prop,clazz = cache[signature];
  121. if(!clazz){
  122. var tname = "dj_lettable_"+(factory.inc++);
  123. var gtname = tname+"_dj_getter";
  124. var cParts = [
  125. "Class "+tname,
  126. " Public data__" // this our reference to the original object
  127. ];
  128. for(i=0, l=props.length; i<l; i++){
  129. prop = props[i];
  130. var type = typeof wrapped[prop];
  131. if(type == 'function' || hiddenFunctions[prop]){ // functions must go in regular properties for delegation:/
  132. cParts.push(" Public " + prop);
  133. }else if(type != 'object'){ // the getters/setters can only be applied to primitives
  134. cParts.push(
  135. " Public Property Let "+prop+"(val)",
  136. " Call "+setName+"(me.data__,\""+prop+"\",val)",
  137. " End Property",
  138. " Public Property Get "+prop,
  139. " "+prop+" = "+getName+"(me.data__,\""+prop+"\")",
  140. " End Property");
  141. }
  142. }
  143. cParts.push("End Class");
  144. cParts.push(
  145. "Function "+gtname+"()",
  146. " Dim tmp",
  147. " Set tmp = New "+tname,
  148. " Set "+gtname+" = tmp",
  149. "End Function");
  150. dojox.lang.lettableWin.vbEval(cParts.join("\n"));
  151. // Put the new class in the cache
  152. cache[signature] = clazz = function(){
  153. return dojox.lang.lettableWin.construct(gtname); // the class can't be accessed, only called, so we have to wrap it with a function
  154. };
  155. }
  156. console.log("starting5");
  157. var newObj = clazz();
  158. newObj.data__ = wrapped;
  159. console.log("starting6");
  160. try {
  161. wrapped.__observable = newObj;
  162. } catch(e){ // some objects are not expando
  163. }
  164. for(i = 0, l = props.length; i < l; i++){
  165. prop = props[i];
  166. try {
  167. var val = wrapped[prop];
  168. }
  169. catch(e){
  170. console.log("error ",prop,e);
  171. }
  172. if(typeof val == 'function' || hiddenFunctions[prop]){ // we can make a delegate function here
  173. newObj[prop] = makeInvoker(newObj,wrapped,prop);
  174. }
  175. }
  176. return newObj;
  177. };
  178. }else{
  179. return function(wrapped){ // do it with getters and setters
  180. if(wrapped.__observable){ // if it already has an observable, use that
  181. return wrapped.__observable;
  182. }
  183. var newObj = wrapped instanceof Array ? [] : {};
  184. newObj.data__ = wrapped;
  185. for(var i in wrapped){
  186. if(i.charAt(0) != '_'){
  187. if(typeof wrapped[i] == 'function'){
  188. newObj[i] = makeInvoker(newObj,wrapped,i); // TODO: setup getters and setters so we can detect when this changes
  189. }else if(typeof wrapped[i] != 'object'){
  190. (function(i){
  191. newObj.__defineGetter__(i,function(){
  192. return onRead(wrapped,i);
  193. });
  194. newObj.__defineSetter__(i,function(value){
  195. return onWrite(wrapped,i,value);
  196. });
  197. })(i);
  198. }
  199. }
  200. }
  201. for(i in hiddenFunctions){
  202. newObj[i] = makeInvoker(newObj,wrapped,i);
  203. }
  204. wrapped.__observable = newObj;
  205. return newObj;
  206. };
  207. }
  208. };
  209. if(!{}.__defineGetter__){
  210. if(dojo.isIE){
  211. // to setup the crazy lettable hack we need to
  212. // introduce vb script eval
  213. // the only way that seems to work for adding a VBScript to the page is with a document.write
  214. // document.write is not always available, so we use an iframe to do the document.write
  215. // the iframe also provides a good hiding place for all the global variables that we must
  216. // create in order for JScript and VBScript to interact.
  217. var frame;
  218. if(document.body){ // if the DOM is ready we can add it
  219. frame = document.createElement("iframe");
  220. document.body.appendChild(frame);
  221. }else{ // other we have to write it out
  222. document.write("<iframe id='dj_vb_eval_frame'></iframe>");
  223. frame = document.getElementById("dj_vb_eval_frame");
  224. }
  225. frame.style.display="none";
  226. var doc = frame.contentWindow.document;
  227. dojox.lang.lettableWin = frame.contentWindow;
  228. doc.write('<html><head><script language="VBScript" type="text/VBScript">' +
  229. 'Function vb_global_eval(code)' +
  230. 'ExecuteGlobal(code)' +
  231. 'End Function' +
  232. '</script>' +
  233. '<script type="text/javascript">' +
  234. 'function vbEval(code){ \n' + // this has to be here to call it from another frame
  235. 'return vb_global_eval(code);' +
  236. '}' +
  237. 'function construct(name){ \n' + // and this too
  238. 'return window[name]();' +
  239. '}' +
  240. '</script>' +
  241. '</head><body>vb-eval</body></html>');
  242. doc.close();
  243. }else{
  244. throw new Error("This browser does not support getters and setters");
  245. }
  246. }
  247. dojox.lang.ReadOnlyProxy =
  248. // summary:
  249. // Provides a read only proxy to another object, this can be
  250. // very useful in object-capability systems
  251. // example:
  252. // | var obj = {foo:"bar"};
  253. // | var readonlyObj = dojox.lang.ReadOnlyProxy(obj);
  254. // | readonlyObj.foo = "test" // throws an error
  255. // | obj.foo = "new bar";
  256. // | readonlyObj.foo -> returns "new bar", always reflects the current value of the original (it is not just a copy)
  257. dojox.lang.makeObservable(function(obj,i){
  258. return obj[i];
  259. },function(obj,i,value){
  260. // just ignore, exceptions don't seem to propagate through the VB stack.
  261. });
  262. });