Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hernan/5bba9e1716482fc180db to your computer and use it in GitHub Desktop.
Save hernan/5bba9e1716482fc180db to your computer and use it in GitHub Desktop.

Revisions

  1. @unspecified unspecified revised this gist Nov 5, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ember_unknown_property_hook.js
    Original 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: 'hi',
    name: 'Bruce Wayne',
    unknownProperty: function(key) {
    return key+" is an unknown property";
    }
    });

    hookedObject.get("name"); // => hi
    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
  2. @unspecified unspecified revised this gist Nov 5, 2013. 1 changed file with 27 additions and 12 deletions.
    39 changes: 27 additions & 12 deletions ember_unknown_property_hook.js
    Original 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
    myObject.get("age"); // age is an unknown property as we did not set "age" yet, the expression on the left evaluates to undefined
    /*
    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
    /*
    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
    /*
    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
    /*
    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
    /*
    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");
  3. @unspecified unspecified revised this gist Nov 5, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions ember_unknown_property_hook.js
    Original 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
    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
    // 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
  4. @unspecified unspecified renamed this gist Nov 5, 2013. 1 changed file with 0 additions and 0 deletions.
  5. @unspecified unspecified revised this gist Nov 5, 2013. 1 changed file with 53 additions and 6 deletions.
    59 changes: 53 additions & 6 deletions ember_unknown_property_hook
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,55 @@
    # Little background about unknown properties
    myObject = Ember.Object.create({name: 'hi'}) # a plain simple ember object
    // 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
    // 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
    // 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
    }
    }
    })
  6. @unspecified unspecified created this gist Nov 5, 2013.
    8 changes: 8 additions & 0 deletions ember_unknown_property_hook
    Original 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