[ 3 posts ]

Joh

  • Username: snilsor
  • Joined: Fri Jul 10, 2009 2:22 am
  • Posts: 14
  • Offline
  • Profile

Using MyClass.ATTRS correctly?

Post Posted: Mon Jul 20, 2009 7:15 am
+1-
Hello YUI3 experts,
coming from YUI2 I want to create my own classes. They should support all the nice automatic event listeners and fireing of events when attributes are changed etc.

1.) my problem: attributes I defined in MyClass.ATTRS are not available in myClass.constructor (as well as in initializer: function(cfg) ). How can that be :?:
Code:
YUI.add('my-examples', function(Y)
{
    Y.namespace('MyNamespace');
    Y.MyNamespace.MyClass = function(myConstructorArgument)
    {
        Y.MyNamespace.MyClass.superclass.constructor.call(this, conf); // apply does not work, must use call
        this.myAttribute.key = 'value'; // does not work. this.myAttribute = NULL  why?
    };
   Y.MyNamespace.MyClass.NAME = "myClass";
   Y.MyNamespace.MyClass.ATTRS =
   {
      myAttribute: {key:"defaultValue"}
   };
   Y.extend(Y.MyNamespace.MyClass, Y.Base,
   {
      initializer: function(cfg)
      {
        this.myAttribute.key = 'newValue'; // does not work, too! this.myAttribute = NULL, why?
      }
   };
},'*',{requires:['base'], skinnable:false});

2.) What is the correct way to get an object of such a class :?: (see code)
Code:
var myNewClass = Y.Base.build("myClass", Y.MyNamespace.MyClass,[ ]);
var myInstance1 = new myNewClass(myConstructorArgument); // it has no attribs from ATTRS

var myNewInstance2 = new Y.MyNamespace.MyClass(myConstructorArgument); // is this in YUI3 class hierarchy allowed? It has as well no attribs from ATTRS

The documentation does not give me a consietnt picture, because the examples from Y.Base docu differ from the 'Getting started: extends' examples.

Thanks for your help.

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: Using MyClass.ATTRS correctly?

Post Posted: Mon Jul 20, 2009 7:54 am
+1-
Joh --

You are missing the fact that Attributes are accessed via getters/setters.

This example works:

Attribute Example

Click here to see the revision history on this Gist.

Joh

  • Username: snilsor
  • Joined: Fri Jul 10, 2009 2:22 am
  • Posts: 14
  • Offline
  • Profile

Re: Using MyClass.ATTRS correctly?

Post Posted: Mon Jul 20, 2009 8:41 am
+0-
;) Thanks.... what a fool I have been.

Concerning my second question:
2.) What is the correct way to get an object of such a class :?: (see code)
Code:
// (1)
var myNewClass = Y.Base.build("myClass", Y.MyNamespace.MyClass,[ ]);
var myInstance1 = new myNewClass(myConstructorArgument);
// (2)
var myInstance2 = new Y.MyNamespace.MyClass(myConstructorArgument); // is this in YUI3 class hierarchy allowed?


As you used the first way to build a new class and instance, i guess the second (easier) way is not supposed to be used?
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum