| Page 1 of 1 | [ 8 posts ] |
|
Hi I want to extend overlay. My code is as follows:
Code: YUI.use("node", "overlay", function (Y) { function Mask(node) { var oDiv = Y.Node.create("<div></div>").setStyles({ opacity: self.opacity || 0.5, zIndex: 9999, backgroundColor: "#fff" }); this.overlay = Mask.superclass.constructor.call({ srcNode: oDiv, visible: false, xy: [node.getX(), node.getY()], width: node.get("offsetWidth"), height: node.get("offsetHeight") }).render(); return this; } Y.extend(Mask, Y.Overlay); }); And I wanted to use like: var mask = new Mask(container); mask.overlay.show() etc. But I am getting error says: R.NAME is undefined IN widget-min.js (line 8) What I am doing Wrong. Please help. Thanks |
|
Take a look at this thread for creating new constructors based on Y.Overlay.
|
|
Many Thanks Nick.
So, Basically we can't extend overlay module as I did, we need to use the widget lifecycle, right ? |
|
Widget lifecycle isn't the most accurate way to describe it; You need to use the Base builder utility to create "Overlay-like" objects.
|
|
You're getting the error because the object you're calling the Overlay constructor with doesn't have a static NAME property associated with its constructor. It's just a plain object, making its constructor Object. It looks like you forgot to pass 'this' as the first argument.
It looks like you're mixing two ideas: 1) composition (aka, "has a"), and 2) extension (aka, "is a"). On the one hand, you want to create an object (Mask instance) that *has a* property which is an instance of Overlay. On the other, you want the Mask instance to *be* an Overlay. Extension should be sufficient. No need to store this.overlay. There is another issue. Namely, you're creating a div off the DOM tree and binding to it as your srcNode and calling render(). I don't recall if Widget is nice enough to attach the srcNode to the document if it isn't already, but I don't think it does. That means your new mask Overlay is off DOM, and therefore will not be visible. Regional modal masking is an interesting use case. Have you looked at the WidgetModality extension? http://yuilibrary.com/yui/docs/api/clas ... ality.html (you can see how it's used in the Panel source and user guide). |
|
Many thanks Luke !!!
I have got it working now Only problem I have now is while extending the existing Mask class: function MaskNew(node, opacity, msg) { Mask.superclass.constructor.call(this, node, opacity); this.msg = msg; return this; } Y.extend(MaskNew, Mask); .................... ..................... Every time I need to assign the static NAME to the constructor like: MaskNew.NAME = "MaskNew"; Otherwise it throws error. Once again thanking you. |
Juan Ignacio DopazoYUI Contributor
|
"return this" it's already implied in constructors. It's not necessary to write it.
What you did is the "manual" way of doing inheritance in YUI. You can also use Y.Base.create. Ie: Code: Y.Mask = Y.Base.create('mask', Y.Overlay, [Y.WidgetModality], { initializer: function (config) { this.msg = config.msg; } }); var mask = new Y.Mask({ msg: 'foo' }); |
|
You're getting an error because your constructor chaining isn't quite right. An easy way to think about it is:
Code: function MyClass(arg1, arg2) { // Where Y.extend(MyClass, MySuperClass); MyClass.superclass.constructor.call(this, arg1, arg2); } is basically like Code: function MyClass(arg1, arg2) { this = new MySuperClass(arg1, arg2); } Except that's pseudo code and totally doesn't work like that in JS in real life But the point being that the long superclass.constructor.call(...) line is basically a call to the superclass, which means you need to pass arguments that make sense if you were calling it directly with `new`. In your case, MaskNew should be calling MaskNew.superclass.constructor(this, node) since MaskNew's superclass is Mask and the Mask constructor expects only one argument. Meanwhile, Mask should call its superclass passing a single configuration object argument because that's what Y.Overlay (and anything Y.Base based) expects. HTH |
| Page 1 of 1 | [ 8 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