rzel / joose-js

Automatically exported from code.google.com/p/joose-js
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

"Persistent" properties (init: [] or init: {}) #20

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Class('CA', {
    has: {
        PROPERTY: {init: {}}
    },
    methods: {
        initialize: function(){
            this.PROPERTY.TEST = "inited by " + this.meta.getName();
        }
    }
});

Class('CB', {
    isa: CA,
    before: {
        initialize: function(){
            console.log(this.PROPERTY.TEST);
        }
    }
});

new CA();
new CB();

What is the expected output? What do you see instead?
expected:
 undefined
instead:
 inited by CA

What version of the product are you using? On what operating system?
 2.1 / windows 7 / Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.1.3)
Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)

Please provide any additional information below.
 properties with type Object (or Array) need to be cloned before
initialization. path:

Index: ./joose.js
===================================================================
--- ./joose.js  (revision 3403)
+++ ./joose.js  (working copy)
@@ -1346,6 +1346,15 @@
             var val = props.init;
             var type = typeof val;

+            if(val && 'object'==type){
+               if('[object Array]'==Object.prototype.toString.call(val))
+                   val=Array.prototype.slice.call(val);
+               else{
+                   var initial=val;
+                   Joose.O.extend(val={}, initial);
+               }
+            }
+
             classObject.prototype[name] = val;
         }
     },

Original issue reported on code.google.com by denis.gu...@gmail.com on 20 Oct 2009 at 8:52

GoogleCodeExporter commented 9 years ago
with persistense:

            if(val && 'object'==type && !props.persistent){
                if('[object Array]'==Object.prototype.toString.call(val))
                    val=Array.prototype.slice.call(val);
                else{
                    var initial=val;
                    Joose.O.extend(val={}, initial);
                }
            }

Original comment by denis.gu...@gmail.com on 20 Oct 2009 at 8:55

GoogleCodeExporter commented 9 years ago
If you need to initialize a property with an object (or array) you are supposed 
to
provide an initialization function:

has : {

  prop : {
      init : function () { return {} }
  }
}

Otherwise such property will be shared among all instances (including 
subclass'es
ones), as you reported.

Please report if this solves this issue.

Original comment by nickol...@gmail.com on 20 Oct 2009 at 9:18

GoogleCodeExporter commented 9 years ago
I try to use this, but my property is private:

_e: {init: {}}

and I didn't use getters/setters in my base component

Original comment by denis.gu...@gmail.com on 20 Oct 2009 at 9:22

GoogleCodeExporter commented 9 years ago
This function will be used for property initialization only, getters/setters are
different things.

Original comment by nickol...@gmail.com on 20 Oct 2009 at 9:31

GoogleCodeExporter commented 9 years ago
Try _e: {init: function () { return {} }}

Original comment by malte.ubl on 20 Oct 2009 at 9:36