Skip to content

Instantly share code, notes, and snippets.

@neonstalwart
Created July 12, 2011 15:36
Show Gist options
  • Save neonstalwart/1078223 to your computer and use it in GitHub Desktop.
Save neonstalwart/1078223 to your computer and use it in GitHub Desktop.
/**
* @license Copyright (c) 2011 Cello Software, LLC.
* All rights reserved.
* Available via the new BSD License.
*/
/*jshint
bitwise: false, curly: true, eqeqeq: true, forin: true, immed: true, indent: 4, maxlen: 100,
newcap: true, noarg: true, noempty: true, onevar: true, passfail: false, undef: true,
white: true
*/
/*global define: false, require: false */
define([
'dojo',
'dijit/_WidgetBase',
'./_base',
'dojo/query!css3'
], function (d, WidgetBase, mustache, query) {
//'use strict';
var dfe = d.forEach,
empty = {};
return d.declare([WidgetBase], {
declaredClass: 'mustache_Templated',
// tEvents:
// {
// '.foo': 'onSomething:handler'
// }
// any nodes that match the selector (.foo) will have the handler (handler) connected to
// the event (onSomething). these are automatically managed when the template is rendered
// or destroyed. this is currently only good for node events - doesn't work for widgets.
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,
// type of node to create as domNode
nodeType: 'div',
buildRendering: function () {
if (!this.domNode) {
this.domNode = this.srcNodeRef || d.create(this.nodeType);
}
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