Own Kinds & Create Component

edited March 2012 in Enyo 2
Hi,

I am taking a JS object and turning it into an enyo.kind (subkind of canvas). I've spent the last couple hours debugging this, but basically, in my kind, I define a bunch of variables, like this:
enyo.kind({
  name: "myKind",
  kind: "Canvas",
  components: [
  ],
  a:{}, 	
  b:"",	
  c:0.5,	
  d:1.0,	
  bb:[], ...etc
When I created the kind, I created up to three:
this.createComponent({kind: myKind, name:name, location:location, container: this});

However, when I went to change the value "bb", ie via this.$[name].bb[0] = ..., this changed "bb" for all the components I created.

Finally, when I either added "bb:[]" to the createComponent line, or added "this.bb = [];" in the create function, the kind responded normally.

Is this how it is supposed to be? Am I missing a key aspect about enyo or javascript?

Cheers,
Andrew

Comments

  • The problem is that in JavaScript, objects and arrays aren't copied when you say "var a = b;", instead you get a reference and a and b refer to the same object in memory.

    You need to make a create method that uses enyo.clone to make a copy of the object in your local object instance, something like
    create: function() {
        this.a = enyo.clone(this.a);
        this.bb = enyo.clone(this.bb);
    },
    enyo.clone is a helper method that makes a true copy of an object or array. It only does a shallow copy, though, so if your default value for a was something like
    { x: 1, y: 2, z: { up: 1, down: 2 } }
    then cloning a would give you fresh copies of the x and y fields, but the cloned z field would be the same object as in the original.
Sign In or Register to comment.