[ 8 posts ]

Aneesh M J

  • Username: aneesh
  • Joined: Thu Aug 05, 2010 3:08 am
  • Posts: 31
  • Offline
  • Profile

Extending Overlay

Post Posted: Fri Nov 18, 2011 5:52 am
+0-
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

Nick Husher

YUI Contributor

  • Offline
  • Profile
Tags:

Re: Extending Overlay

Post Posted: Fri Nov 18, 2011 9:27 am
+0-
Take a look at this thread for creating new constructors based on Y.Overlay.

Aneesh M J

  • Username: aneesh
  • Joined: Thu Aug 05, 2010 3:08 am
  • Posts: 31
  • Offline
  • Profile

Re: Extending Overlay

Post Posted: Mon Nov 21, 2011 4:25 am
+0-
Many Thanks Nick.

So, Basically we can't extend overlay module as I did, we need to use the widget lifecycle, right ?

Nick Husher

YUI Contributor

  • Offline
  • Profile

Re: Extending Overlay

Post Posted: Mon Nov 21, 2011 7:58 am
+0-
Widget lifecycle isn't the most accurate way to describe it; You need to use the Base builder utility to create "Overlay-like" objects.

Luke Smith

YUI Contributor

  • Username: lsmith
  • Joined: Thu Aug 28, 2008 7:50 am
  • Posts: 507
  • Location: Sunnyvale
  • Twitter: ls_n
  • GitHub: lsmith
  • Gists: lsmith
  • IRC: ls_n
  • YUI Developer
  • Offline
  • Profile

Re: Extending Overlay

Post Posted: Mon Nov 21, 2011 10:03 pm
+0-
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).

Aneesh M J

  • Username: aneesh
  • Joined: Thu Aug 05, 2010 3:08 am
  • Posts: 31
  • Offline
  • Profile
Tags:

Re: Extending Overlay

Post Posted: Wed Nov 23, 2011 5:28 am
+0-
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 Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 619
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: Extending Overlay

Post Posted: Wed Nov 23, 2011 8:51 am
+0-
"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'
});

Luke Smith

YUI Contributor

  • Username: lsmith
  • Joined: Thu Aug 28, 2008 7:50 am
  • Posts: 507
  • Location: Sunnyvale
  • Twitter: ls_n
  • GitHub: lsmith
  • Gists: lsmith
  • IRC: ls_n
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: Extending Overlay

Post Posted: Wed Nov 23, 2011 11:24 am
+0-
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
  [ 8 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