[ 6 posts ]

Kamil T.

  • Joined: Wed Dec 10, 2008 2:16 am
  • Posts: 13
  • Location: Cracow, Poland
  • Offline
  • Profile

readOnly property doesn't work properly?

Post Posted: Sat Dec 20, 2008 7:59 am
+0-
Code:
        var TestClass = function (config) {
                TestClass.superclass.constructor.apply(this, arguments);
            };

        TestClass.NAME = "testClass";

        TestClass.ATTRS = {
            testAttr1: {
                readOnly: true
            },
            testAttr2: {}
        };

        Y.extend(TestClass, Y.Base, {
            testAll: function () {
                alert(this.get("testAttr1"));
                alert(this.get("testAttr2"));
            }
        });

        var testClass = new TestClass({
                testAttr1: "test #1",
                testAttr2: "test #2"
            });

        testClass.testAll();

First alert returns "undefined", second returns "test #2". Is it supposed to work like that? My guess would be that "readOnly" property only makes attribute value immutable (with set() method) - not hidden.
I want to set this value and protect it from being changed ever after (even once) - how should I implement it?

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
Tags:

Re: readOnly property doesn't work properly?

Post Posted: Mon Dec 22, 2008 7:58 am
+0-
You probably want writeOnce, not readOnly.
http://developer.yahoo.com/yui/3/api/Attribute.html#method_addAtt
Try this:

Code:
            testAttr1: {
                writeOnce: true
            },
--
Dav

Kamil T.

  • Joined: Wed Dec 10, 2008 2:16 am
  • Posts: 13
  • Location: Cracow, Poland
  • Offline
  • Profile
Tags:

Re: readOnly property doesn't work properly?

Post Posted: Mon Dec 22, 2008 12:27 pm
+0-
Ah! Nevermind about that writeOnce thing - it works as expected - I must did something wrong when I tried it before.

But it looks like readOnly is buggy anyway:

Code:
var TestClass = function (config) {
        TestClass.superclass.constructor.apply(this, arguments);
    };

TestClass.NAME = "testClass";
TestClass.ATTRS = {
    testAttr: {
        readOnly: true
    }
};

Y.extend(TestClass, Y.Base);

var testClass = new TestClass({
        testAttr: "test value"
    });
alert(testClass.get("testAttr"));

testClass.set("testAttr", "test value overwrite");
alert(testClass.get("testAttr"));

testClass.set("testAttr", "test value second overwrite");
alert(testClass.get("testAttr"));


Results:
alert #1: undefined
alert #2: test value overwrite
alert #3: test value overwrite

It looks like either alert #1 is correct or alert #2 - either we should be able to set value to undefined read-only config propert (only once) or we should not. Right now it's a little messy, because it is not allowed when instantiating object, and is allowed with .set() method later.

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: readOnly property doesn't work properly?

Post Posted: Mon Dec 22, 2008 12:40 pm
+0-
That would be because you didn't specify a default value. readOnly expects a value, so it kicked in after the first set..

Code:
testAttr1: {
                value: 'Non write value',
                readOnly: true
},
--
Dav

Kamil T.

  • Joined: Wed Dec 10, 2008 2:16 am
  • Posts: 13
  • Location: Cracow, Poland
  • Offline
  • Profile

Re: readOnly property doesn't work properly?

Post Posted: Mon Dec 22, 2008 1:14 pm
+0-
Ok, so readOnly expects default value and when default value is not set then it behaves like writeOnce - yes?

Because if so, then we should be able to set this value either while initializing new class instance, like this:
Code:
var testClass = new TestClass({
        testAttr: "test value"
    });
alert(testClass.get("testAttr"));

But we are not able to (it returns undefined).

Or we should be able to set this property later, like this:
Code:
testClass.set("testAttr", "test value overwrite");
alert(testClass.get("testAttr")); // returns "test value overwrite"

And this works as expected - value is set.

Either I am missing some very obvious thing here or I am unable to accurately describe this issue :-) Or everything is ok and I just don't understand logic behind - well, it's ok then ;-)

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
Tags:

Re: readOnly property doesn't work properly?

Post Posted: Mon Dec 22, 2008 1:16 pm
+0-
If it fails your logic test, the please feel free to file a bug ;)

http://developer.yahoo.com/yui/3/attribute/#filingbugs
--
Dav
  [ 6 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