Skip to content

Instantly share code, notes, and snippets.

@ericf
Created December 31, 2009 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericf/266915 to your computer and use it in GitHub Desktop.
Save ericf/266915 to your computer and use it in GitHub Desktop.
/**
* MyWidget
*/
var MyWidget,
MY_WIDGET = 'myWidget',
SUBJECT = 'subject',
MESSAGE = 'message',
SUBJECT_NODE = 'subjectNode',
MESSAGE_NODE = 'messageNode',
UI_SRC = Y.Widget.UI_SRC,
isNull = Y.Lang.isNull,
isString = Y.Lang.isString;
// *** Constructor *** //
MyWidget = function () {
MyWidget.superclass.constructor.apply(this, arguments);
};
// *** Static *** //
Y.mix(MyWidget, {
NAME : MY_WIDGET,
ATTRS : {
subject : {
value : null,
setter : '_setNewSubject'
},
message : {
value : null,
setter : '_setNewMessage'
},
subjectNode : {
writeOnce : true
},
messageNode : {
writeOnce : true
},
}
});
// *** Prototype *** //
Y.extend(MyWidget, Y.Widget, {
// *** Instance Members *** //
// *** Lifecycle Methods *** //
initializer : function (config) {},
destructor : function () {},
renderUI : function () {},
bindUI : function () {
var subjectNode = this.get(SUBJECT_NODE),
messageNode = this.get(MESSAGE_NODE);
this._bindAttrUI([SUBJECT, MESSAGE]);
if (subjectNode) {
subjectNode.on('change', Y.bind(function(e){
this.set(SUBJECT, e.target.get('value'), { src: UI_SRC });
}, this));
}
if (messageNode) {
messageNode.on('change', Y.bind(function(e){
this.set(MESSAGE, e.target.get('value'), { src: UI_SRC });
}, this));
}
},
syncUI : function () {
this._syncAttrUI([SUBJECT, MESSAGE]);
},
// *** Public Methods *** //
syncAttrs : function () {
var subjectNode = this.get(SUBJECT_NODE),
messageNode = this.get(MESSAGE_NODE);
if (subjectNode) {
this.set(SUBJECT, subjectNode.get('value'), { src: UI_SRC });
}
if (messageNode) {
this.set(MESSAGE, messageNode.get('value'), { src: UI_SRC });
}
},
resetData : function () {
this.reset(SUBJECT);
this.reset(MESSAGE);
},
// *** Private Methods *** //
_setNewSubject : function (v) {
if (isNull(v)) {
return v;
}
if (isString(v)) {
return ( v.length > 0 ? v : null );
}
return Y.Attribute.INVALID_VALUE;
},
_setNewMessage : function (v) {
if (isNull(v)) {
return v;
}
if (isString(v)) {
return ( v.length > 0 ? v : null );
}
return Y.Attribute.INVALID_VALUE;
}
_uiSetSubject : function (v, src) {
if (src === UI_SRC) {
return;
}
var subjectNode = this.get(SUBJECT_NODE);
if (subjectNode) {
subjectNode.set(VALUE, v || '');
}
},
_uiSetMessage : function (v, src) {
if (src === UI_SRC) {
return;
}
var messageNode = this.get(MESSAGE_NODE);
if (messageNode) {
messageNode.set(VALUE, v || '');
}
}
});
Y.MyWidget = MyWidget;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment