Forked from mudassir0909/ember_unknown_property_hook.js
          
        
    
          Last active
          August 29, 2015 14:17 
        
      - 
      
- 
        Save hernan/5bba9e1716482fc180db to your computer and use it in GitHub Desktop. 
Revisions
- 
        unspecified revised this gist Nov 5, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,13 +21,13 @@ myObject.set("status", undefined); // although myObject.get("status") evaulates instead of returning undefined the result of the unknownProperty hook is returned */ var hookedObject = Ember.Object.create({ name: 'Bruce Wayne', unknownProperty: function(key) { return key+" is an unknown property"; } }); hookedObject.get("name"); // => Bruce Wayne hookedObject.get("randomKey"); // => randomKey is an unknown property hookedObject.get("age"; // => age is an unknown property hookedObject.set("age", 22); // age is now a known property 
- 
        unspecified revised this gist Nov 5, 2013 . 1 changed file with 27 additions and 12 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,25 @@ // Little background about unknown properties var myObject = Ember.Object.create({name: 'hi'}); // a plain simple ember object /* all the properties defined on the object are known properties so the property "name" is a known property for myObject, but any other properties which are not defined on this object are unknown properties */ // age is an unknown property as we did not set "age" yet myObject.get("age"); // => undefined /* Be careful ! only properties on the object that are not "set" yet are unknown properties, but not the properties that evaluate to null/undefined */ myObject.set("status", undefined); // although myObject.get("status") evaulates to undefined, status is a known property for myObject /* unknownProperty hook Basically when we do a "get" on a unknown property, instead of returning undefined the result of the unknownProperty hook is returned */ var hookedObject = Ember.Object.create({ name: 'hi', unknownProperty: function(key) { @@ -23,8 +33,10 @@ hookedObject.get("age"; // => age is an unknown property hookedObject.set("age", 22); // age is now a known property hookedObject.get("age"); // => 22 /* setUnknownProperty hook it gets called when we are trying to do a set on an unknown property */ var baseObject = Ember.Object.create(); var hookedObject = Ember.Object.create({ setUnknownProperty: function(key, value){ @@ -34,10 +46,13 @@ var hookedObject = Ember.Object.create({ hookedObject.set("name", "Bruce"); baseObject.get("name"); // => Bruce /* We are basically proxying the set property to other object, Reminds you of something ?... Enter Ember.ObjectProxy `Ember.ObjectProxy` forwards all properties not defined by the proxy itself to a proxied `content` object. (from the source code) Let's implement a similar proxy object of our own using the above concepts */ var myProxyObject = Ember.Object.create({ unknownProperty: function(key){ var content = this.get("content"); 
- 
        unspecified revised this gist Nov 5, 2013 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ // Little background about unknown properties var myObject = Ember.Object.create({name: 'hi'}); // a plain simple ember object // all the properties defined on the object are known properties //so the property "name" is a known property for myObject, but any other properties which are not defined on this object are unknown properties myObject.get("age"); // age is an unknown property as we did not set "age" yet, the expression on the left evaluates to undefined // Be careful ! only properties on the object that are not "set" yet are unknown properties but not the properties that evaluate to null/undefined 
- 
        unspecified renamed this gist Nov 5, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        unspecified revised this gist Nov 5, 2013 . 1 changed file with 53 additions and 6 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,55 @@ // Little background about unknown properties var myObject = Ember.Object.create({name: 'hi'}); # a plain simple ember object // so the property "name" is a known property for myObject, but any other properties which are not defined on this object are unknown properties myObject.get("age"); // age is an unknown property as we did not set "age" yet, the expression on the left evaluates to undefined // Be careful ! only properties on the object that are not "set" yet are unknown properties but not the properties that evaluate to null/undefined myObject.set("status", undefined); // although myObject.get("status") evaulates to undefined, status is a known property for myObject // unknownProperty hook // Basically when we do a "get" on a unknown property, instead of returning undefined the result of the unknownProperty hook is returned var hookedObject = Ember.Object.create({ name: 'hi', unknownProperty: function(key) { return key+" is an unknown property"; } }); hookedObject.get("name"); // => hi hookedObject.get("randomKey"); // => randomKey is an unknown property hookedObject.get("age"; // => age is an unknown property hookedObject.set("age", 22); // age is now a known property hookedObject.get("age"); // => 22 // setUnknownProperty hook // it gets called when we are trying to do a set on an unknown property var baseObject = Ember.Object.create(); var hookedObject = Ember.Object.create({ setUnknownProperty: function(key, value){ baseObject.set(key, value); } }); hookedObject.set("name", "Bruce"); baseObject.get("name"); // => Bruce // We are basically proxying the set property to other object, Reminds you of something ?...Enter Ember.ObjectProxy // `Ember.ObjectProxy` forwards all properties not defined by the proxy itself // to a proxied `content` object. (from the source code) // Let's implement a similar proxy object of our own using the above concepts var myProxyObject = Ember.Object.create({ unknownProperty: function(key){ var content = this.get("content"); if(content){ return content.get(key); } }, setUnknownProperty: function(key, value){ var content = this.get("content"); if(content){ return content.set(key, value); }else{ // raise an error saying cannot set key, value as the content property is undefined } } }) 
- 
        unspecified created this gist Nov 5, 2013 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ # Little background about unknown properties myObject = Ember.Object.create({name: 'hi'}) # a plain simple ember object # so the property "name" is a known property for myObject, but any other properties which are not defined on this object are unknown properties myObject.get("age") # age is an unknown property as we did not set "age" yet, the expression on the left evaluates to undefined # Be careful ! only properties on the object that are not "set" yet are unknown properties but not the properties that evaluate to null/undefined myObject.set("status", undefined) # although myObject.get("status") evaulates to undefined, status is a known property for myObject