| Page 1 of 1 | [ 3 posts ] |
|
Hello ladies and gentlemen.
I'm trying to create an extremely simple widget to continue my (constantly stalling) learning YUI experience. I'm scratched my head, huffed AND puffed but can't figure out why I keep being told that "Widget is not undefined". Any help hugely appreciated ... The Widget of Despair Code: YUI.add("collapsable-panel", function(Y){ CollapsablePanel = function(config){ CollapsablePanel.superclass.constructor.apply(this, arguments); } CollapsablePanel.NAME = "collpsablePanel"; CollapsablePanel.ATTRS = { open: { value: false } }; CollapsablePanel.HTML_PARSER = { toggle: "", content: "" }; Y.extend(CollapsablePanel, Widget, { initializer: function(config){ } }); }, "0.0.1", {requires: ["widget"]}); The Implementation of Hope Code: YUI({ modules: { "collapsable-panel": { fullpath: "/resources/js/widgets/collapsable-panel.js", type: "js", requires: ["widget"] } } }).use("collapsable-panel", function(Y){ x = new Y.CollapsablePanel(); }); |
Juan Ignacio DopazoYUI Contributor
|
You're using YUI right. The Loader part is ok and the module structure is also ok. The problem isn't in YUI but in how variables and object properties work.
When you create a module with YUI.add you don't get for free all the YUI classes inside the function. You get them as properties of the Y variable. So Widget is undefined because it should be Y.Widget, a property of Y. That is also valid for your own class. You need to add it as a property of Y at some point to be able to reference it later. So you need to do Y.CollapsablePanel = CollapsablePanel somewhere after you declare it as a variable. Which leads me to the last comment... Always use the "var" keyword for declaring variables. Otherwise all your variables will end up being global and that can have very unexpected consequences. Here's a working example: http://jsfiddle.net/juandopazo/Ts69w/ Last edited by jdopazo on Mon May 07, 2012 5:23 am, edited 1 time in total. |
|
Thank a zillion Juan. That all makes perfect sense.
I've been haunted by ghost of scope before. The "x" variable was just a quick and dirty thing to see whether I was getting anything back from the constructor. |
| Page 1 of 1 | [ 3 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