Skip to content

Instantly share code, notes, and snippets.

@neonstalwart
Created May 31, 2011 18:14
Show Gist options
  • Save neonstalwart/1001007 to your computer and use it in GitHub Desktop.
Save neonstalwart/1001007 to your computer and use it in GitHub Desktop.
define([
'dojo',
'dijit/_Widget',
'./_base',
'dojo/query!css3'
], function (d, _Widget, mustache, query) {
//'use strict';
var dfe = d.forEach,
empty = {};
return d.declare([_Widget], {
declaredClass: 'mustache_Templated',
// tEvents:
// {
// '.foo': 'onSomething:handler'
// }
// any nodes that match the selector (.foo) will have the handler (handler) connected to
// the event (onSomething).
tEvents: null,
// this is an array of ids which will map to attach points. the name in the array will
// be prefixed with the widget's id + '-'.
// for example, if the widget's id is 'widgetFoo' and tPoints is ['bar'] then
// this.bar will be the node with id 'widgetFoo-bar'.
tPoints: null,
buildRendering: function () {
this.inherited(arguments);
this.render();
},
render: function () {
var node,
template = d.trim(mustache.to_html(this.templateString, this, this.partials || {}));
this.unrender();
d.place(template, this.domNode, 'only');
this.attachEvents();
this.attachPoints();
},
unrender: function () {
this.detachEvents();
this.detachPoints();
this.destroyDescendants();
d.empty(this.domNode);
},
attachEvents: function () {
var sel,
tEvents = this.tEvents || {},
tConnects = this._tConnects = [];
for (sel in tEvents) {
if (!(sel in empty)) {
tConnects.push.apply(tConnects, query(sel, this.domNode).map(function (n) {
var connection = tEvents[sel].split(':');
return d.connect(n, connection[0], this, connection[1]);
}, this));
}
}
},
attachPoints: function () {
dfe(this.tPoints, function (p) {
// query like this is the only way i found to query nodes outside the document
this[p] = query('[id=' + this.id + '-' + p + ']', this.domNode)[0];
}, this);
},
detachEvents: function () {
dfe(this._tConnects || [], d.disconnect);
},
detachPoints: function () {
dfe(this.tPoints, function (p) {
delete this[p];
}, this);
},
destroyRendering: function () {
this.unrender();
this.inherited(arguments);
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment