Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Basic YUI Widget Structure.
/* global YUI */
YUI.add("my-module", function (Y) {
"use strict";
Y.MyWidget = Y.Base.create("my-module", Y.Widget, [], {
initializer: function () {
// publish any events
// do any instantiation that doesn't require DOM
},
renderUI: function () {
// create all DOM nodes
var title = Y.Node.create("<div></div>"),
button = Y.Node.create("<div>Click Me!</div>");
// use this.getClassName(arg1s...) to create unque classes
title.addClass(this.getClassName("title"));
button.addClass(this.getClassName("button"));
// add nodes to the page all at once
this.get("contentBox").append(Y.all([title, button]));
// store shortcuts for DOM you'll need to reference
this._titleNode = title;
this._buttonNode = button;
},
bindUI: function () {
// store references to event handlers you set on other
// objects for clean up later
this._buttonClickHandler = this._buttonNode.on("click", function (event) {
Y.log("you clicked the button!");
event.halt();
}, this);
// assign listeners to events on the instance directly
// they're cleaned up automatically
this.after("titleChange", this._afterTitleChange, this);
},
_afterTitleChange: function (event) {
this._titleNode.setContent(event.newVal);
},
syncUI: function () {
// now that the DOM is created, syncUI sets it up
// given the current state of our ATTRS
this._afterTitleChange({
newVal: this.get("title")
});
},
destructor: function () {
if (this.get("rendered")) {
// bindUI was called, clean up events
this._buttonClickHandler.detach();
this._buttonClickHandler = null;
// renderUI was called, clean up shortcuts
this._titleNode = this._buttonNode = null;
}
}
}, {
// Public attributes that broadcast change events
ATTRS: {
title: {
value: "No one gave me a title :("
}
},
// Attributes whose default values can be scraped from HTML
HTML_PARSER: {
title: function (srcNode) {
return srcNode.getAttribute("title");
}
}
});
}, "3.3.0", {
requires: [
"base-build", // provides Y.Base.create
"widget" // provides Y.Widget
],
group: "nfl", // declares the nfl group (important for skins)
skinnable: true // declares that your module is skinned
});
@Ry7n

This comment has been minimized.

Show comment Hide comment
@Ry7n

Ry7n Oct 13, 2010

Backing up my comment on this post.

Owner

Ry7n commented Oct 13, 2010

Backing up my comment on this post.

@lsmith

This comment has been minimized.

Show comment Hide comment
@lsmith

lsmith Dec 2, 2011

I recommend extending Base via Y.Base.create. See my YUIConf vid for details: http://www.youtube.com/watch?v=_zhQIfT7g58

lsmith commented Dec 2, 2011

I recommend extending Base via Y.Base.create. See my YUIConf vid for details: http://www.youtube.com/watch?v=_zhQIfT7g58

@Ry7n

This comment has been minimized.

Show comment Hide comment
@Ry7n

Ry7n Dec 14, 2011

Thanks. Updated the example to a more thorough description of Y.Widget.

Owner

Ry7n commented Dec 14, 2011

Thanks. Updated the example to a more thorough description of Y.Widget.

@JimThorpeNZ

This comment has been minimized.

Show comment Hide comment
@JimThorpeNZ

JimThorpeNZ Jul 29, 2014

As an absolute beginner to YUI, this page has been very useful, for other beginners coming across this page, you'll need to call it using something like this (feel free to edit this if it's not quite right):

YUI().use("my-module", function (Y) {
var MyWidget = new Y.MyWidget();
MyWidget.render();
});

As an absolute beginner to YUI, this page has been very useful, for other beginners coming across this page, you'll need to call it using something like this (feel free to edit this if it's not quite right):

YUI().use("my-module", function (Y) {
var MyWidget = new Y.MyWidget();
MyWidget.render();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment