Skip to content

Instantly share code, notes, and snippets.

@dacrazycoder
Last active December 22, 2015 08:09
Show Gist options
  • Save dacrazycoder/6443109 to your computer and use it in GitHub Desktop.
Save dacrazycoder/6443109 to your computer and use it in GitHub Desktop.
Overloading the dojo/app/view's loadTemplate function to support loading urls as part of the template
// Overload the view loader..
View.prototype._loadTemplate = function(){
var localView = this;
// summary:
// load view HTML template and dependencies.
// tags:
// private
//
var tpl = this.template;
var deps = this.dependencies?this.dependencies:[];
if(this.templateString){
return true;
}else if (tpl.indexOf("$") == 0){
var url = tpl.substring(1, tpl.length);
var requestDeferred = request.get(url);
requestDeferred.then(function _success(data) {
localView.templateString = data;
},
function _error(error) {
localView.templateString = "<div>Error: "+error+"</div>";
});
var loadViewDeferred = new Deferred();
when(requestDeferred, lang.hitch(this, function() {
loadViewDeferred.resolve(this);
}));
return loadViewDeferred;
}else{
if(tpl){
if(tpl.indexOf("./") == 0){
tpl = "app/"+tpl;
}
deps = deps.concat(["dojo/text!"+tpl]);
}
var def = new Deferred();
if(deps.length > 0){
var requireSignal;
try{
requireSignal = require.on("error", lang.hitch(this, function(error){
if(def.isResolved() || def.isRejected()){
return;
}
if(error.info[0] && error.info[0].indexOf(this.template) >= 0 ){
def.resolve(false);
requireSignal.remove();
}
}));
require(deps, function(){
def.resolve.call(def, arguments);
requireSignal.remove();
});
}catch(e){
def.resolve(false);
if(requireSignal){
requireSignal.remove();
}
}
}else{
def.resolve(true);
}
var loadViewDeferred = new Deferred();
when(def, lang.hitch(this, function(){
this.templateString = this.template ? arguments[0][arguments[0].length - 1] : "<div></div>";
loadViewDeferred.resolve(this);
}));
return loadViewDeferred;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment