Passing data to Kind constructor via layout

edited March 2012 in Enyo 2
How can I do something like this:
{kind: "MyKind", someField: "someData"}
And then in the MyKind type:
constructor: function(){
   this.someFunction(someField);
   this.inherited(arguments);
}
I don't quite understand whether you can define your own attributes and how you might get at them.

Comments

  • edited March 2012
    Any property value you set is available in your object instance. You just need to use "this" to access the data. E.g.
    constructor: function(){
       this.someFunction(this.someField);
       this.inherited(arguments);
    }
  • Duh. Where is the recommended place to define Kind specific variables? The constructor? My background is Java so I'm trying to fit my existing OO knowledge into Enyo.
  • Javascript is highly dynamic, so you don't have to define them at all. If you want to assign a default value you can do it directly in the prototype. If the default is object-valued, then you need to do it in the constructor.
    
    enyo.kind({
      name: "MyKind",
      myvar: 3, // directly in the prototype
      constructor: function() {
        this.myObject = { data: 42 }; // object value, one per instance
        this.inherited(arguments);
      }
    });
  • edited March 2012
    From what I understand (could very well be wrong), if you want to do this:
    {kind: "MyKind", someField: "someData"}
    You can do something like this in your kind:
    enyo.kind({
      name: "MyKind",
      published: {
        someField: null
      }
  • If you want getters and setters and *Changed notifications, you have to implement it as @quindo illustrated with the published hash. If you just want a means to pass data (or store related data), you can set it on the kind declaration or instance.
    enyo.kind({
      name:"MyKind",
      published:{
        someField:0
      },
      someFieldChanged:function(oldValue) {
        // you can validate the new value or do other processing based on the change
      }
      // no way to check for anotherField changing
    });
    
    enyo.kind({
      name:"App",
      components:[
        {name:"myKind", kind:"MyKind", someField:123, anotherField:"abc"}
      ],
      create:function() {
        this.inherited(arguments);
        this.$.myKind.setSomeField(456);
        this.$.myKind.anotherField = "def";
      }
    });
    or live ... http://jsfiddle.net/GN8qr/1/
Sign In or Register to comment.