| Page 1 of 1 | [ 6 posts ] |
|
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? |
|
You probably want writeOnce, not readOnly.
--http://developer.yahoo.com/yui/3/api/Attribute.html#method_addAtt Try this: Code: testAttr1: { writeOnce: true }, Dav |
|
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. |
|
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 |
|
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 |
|
If it fails your logic test, the please feel free to file a bug
--http://developer.yahoo.com/yui/3/attribute/#filingbugs Dav |
| Page 1 of 1 | [ 6 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group