diff --git a/examples/shorthand/main.js b/examples/shorthand/main.js index 636b2bf..c2ee500 100644 --- a/examples/shorthand/main.js +++ b/examples/shorthand/main.js @@ -25,7 +25,7 @@ $(function(){ $('#btnPlain').click(function(){ // Shorthand syntax allows you to retrieve the template, and attach to target DOM element all in one call - infuser.infuse("HelloWorld", $("#targetPlain"), { postRender: togglePlain }); + infuser.infuse("HelloWorld", { postRender: togglePlain, targetSelector: "#targetPlain" }); }); $('#btnFancy').click(function(){ @@ -33,8 +33,8 @@ $(function(){ infuser .infuse( "Example", - $("#targetFancy"), { + targetSelector: "#targetFancy", model: model, preRender: function(target, template) { $(target).children().remove().end().fadeOut().hide(); }, render: function(target, template) { $(target).append(template).slideDown('slow'); }, diff --git a/examples/trafficcop/main.js b/examples/trafficcop/main.js index 6e46993..3fe4f73 100644 --- a/examples/trafficcop/main.js +++ b/examples/trafficcop/main.js @@ -2,8 +2,8 @@ var toggled = false, doStuff = function(modelNum) { infuser.infuse( "Example", - $("#target"), { + targetSelector: "#target", model: menus[modelNum], render: function(target, template) { $(target).append(template).slideDown('slow'); diff --git a/lib/infuser.js b/lib/infuser.js index b26ee3d..43cd0e3 100644 --- a/lib/infuser.js +++ b/lib/infuser.js @@ -130,7 +130,8 @@ var infuser = { templateUrl: "", templateSuffix: ".html", templatePrefix: "", - renderInstruction: function(template, model) { return template; } // NO_OP + renderInstruction: function(template, model) { return template; }, // NO_OP + domTargetResolver: function(templateId) { return "#" + templateId } // DEFAULT MAPPING }, get: function(templateId, callback) { @@ -187,15 +188,16 @@ var infuser = { return template; }, - infuse: function(templateId, targetDomElement, renderOptions) { + infuse: function(templateId, renderOptions) { var options = $.extend({}, defaultRenderOptions, renderOptions), - self = this; + self = this, + targetElement = options.targetSelector || self.config.domTargetResolver(templateId); self.get(templateId, function(template) { var _template = template; - options.preRender(targetDomElement, _template); + options.preRender(targetElement, _template); _template = self.config.renderInstruction(_template, options.model); - options.render(targetDomElement, _template); - options.postRender(targetDomElement); + options.render(targetElement, _template); + options.postRender(targetElement); }); } }; diff --git a/lib/infuser.min.gz.js b/lib/infuser.min.gz.js index 95668f2..e74bb56 100644 Binary files a/lib/infuser.min.gz.js and b/lib/infuser.min.gz.js differ diff --git a/lib/infuser.min.js b/lib/infuser.min.js index b4c7ffe..c21ce8a 100644 --- a/lib/infuser.min.js +++ b/lib/infuser.min.js @@ -1 +1 @@ -(function(a,b){var c=function(){},d={preRender:c,render:function(a,b){$(a).children().length===0?$(a).append($(b)):$(a).children().replaceWith($(b))},postRender:c},e={templates:{},storeTemplate:function(a,b){this.templates[a]=b},getTemplate:function(a){return this.templates[a]},purge:function(){this.templates={}}},f={templateIds:[],storeTemplate:function(a,b){var c=document.getElementById(a);c===null&&(this.templateIds.push(a),c=document.createElement("script"),c.type="text/html",c.id=a,document.body.appendChild(c)),c.text=b},getTemplate:function(a){return document.getElementById(a)},purge:function(){for(var a=0;aThe template {TEMPLATEID} could not be loaded. {STATUS}",h=function(a,b,c){return g.replace("{STATUS}",a).replace("{TEMPLATEID}",b).replace("{TEMPLATEURL}",c)},i=[],j={getTemplatePath:function(a){var c=l.config.templatePrefix+a+l.config.templateSuffix;return l.config.templateUrl===b||l.config.templateUrl===""?c:l.config.templateUrl+"/"+c},templateGetSuccess:function(a,b){return function(c){l.store.storeTemplate(a,c),b(l.store.getTemplate(a))}},templateGetError:function(a,b,c){return function(d){$.inArray(a,i)===-1&&i.push(a);var e=h("HTTP Status code: "+d.status,a,b);l.store.storeTemplate(a,e),c(l.store.getTemplate(a))}}},k={inProgress:{},direct:function(a){var b=a.type+"_"+a.dataType+"_"+a.url,c=this;if(!c.inProgress[b]){var d=function(){delete c.inProgress[b]},e={successCallbacks:[a.success],errorCallbacks:[a.error],success:function(a){$.each($(c.inProgress[b].successCallbacks),function(b,c){c(a)}),d()},error:function(a){$.each($(c.inProgress[b].errorCallbacks),function(b,c){c(a)}),d()}};c.inProgress[b]=$.extend({},a,e),$.ajax(c.inProgress[b])}else c.inProgress[b].successCallbacks.push(a.success),c.inProgress[b].errorCallbacks.push(a.error)}},l={storageOptions:{hash:e,script:f},store:e,config:{templateUrl:"",templateSuffix:".html",templatePrefix:"",renderInstruction:function(a,b){return a}},get:function(a,b){var c=this.store.getTemplate(a),d,e;!c||$.inArray(a,i)!==-1?(d=j.getTemplatePath(a),e={async:!0,url:d,dataType:"html",type:"GET",success:j.templateGetSuccess(a,b),error:j.templateGetError(a,d,b)},k.direct(e)):b(c)},getSync:function(a){var b=this.store.getTemplate(a),c,d,e;if(!b||$.inArray(a,i)!==-1)c=j.getTemplatePath(a),d=null,e={async:!1,url:c,dataType:"html",type:"GET",success:function(a){d=a},error:function(b){$.inArray(a)===-1&&i.push(a),d=h("HTTP Status code: exception.status",a,c)}},$.ajax(e),d===null?d=h("An unknown error occurred.",a,c):(this.store.storeTemplate(a,d),b=this.store.getTemplate(a));return b},infuse:function(a,b,c){var e=$.extend({},d,c),f=this;f.get(a,function(a){var c=a;e.preRender(b,c),c=f.config.renderInstruction(c,e.model),e.render(b,c),e.postRender(b)})}};a.infuser=l})(window) \ No newline at end of file +(function(a,b){var c=function(){},d={preRender:c,render:function(a,b){$(a).children().length===0?$(a).append($(b)):$(a).children().replaceWith($(b))},postRender:c},e={templates:{},storeTemplate:function(a,b){this.templates[a]=b},getTemplate:function(a){return this.templates[a]},purge:function(){this.templates={}}},f={templateIds:[],storeTemplate:function(a,b){var c=document.getElementById(a);c===null&&(this.templateIds.push(a),c=document.createElement("script"),c.type="text/html",c.id=a,document.body.appendChild(c)),c.text=b},getTemplate:function(a){return document.getElementById(a)},purge:function(){for(var a=0;aThe template {TEMPLATEID} could not be loaded. {STATUS}",h=function(a,b,c){return g.replace("{STATUS}",a).replace("{TEMPLATEID}",b).replace("{TEMPLATEURL}",c)},i=[],j={getTemplatePath:function(a){var c=l.config.templatePrefix+a+l.config.templateSuffix;return l.config.templateUrl===b||l.config.templateUrl===""?c:l.config.templateUrl+"/"+c},templateGetSuccess:function(a,b){return function(c){l.store.storeTemplate(a,c),b(l.store.getTemplate(a))}},templateGetError:function(a,b,c){return function(d){$.inArray(a,i)===-1&&i.push(a);var e=h("HTTP Status code: "+d.status,a,b);l.store.storeTemplate(a,e),c(l.store.getTemplate(a))}}},k={inProgress:{},direct:function(a){var b=a.type+"_"+a.dataType+"_"+a.url,c=this;if(!c.inProgress[b]){var d=function(){delete c.inProgress[b]},e={successCallbacks:[a.success],errorCallbacks:[a.error],success:function(a){$.each($(c.inProgress[b].successCallbacks),function(b,c){c(a)}),d()},error:function(a){$.each($(c.inProgress[b].errorCallbacks),function(b,c){c(a)}),d()}};c.inProgress[b]=$.extend({},a,e),$.ajax(c.inProgress[b])}else c.inProgress[b].successCallbacks.push(a.success),c.inProgress[b].errorCallbacks.push(a.error)}},l={storageOptions:{hash:e,script:f},store:e,config:{templateUrl:"",templateSuffix:".html",templatePrefix:"",renderInstruction:function(a,b){return a},domTargetResolver:function(a){return"#"+a}},get:function(a,b){var c=this.store.getTemplate(a),d,e;!c||$.inArray(a,i)!==-1?(d=j.getTemplatePath(a),e={async:!0,url:d,dataType:"html",type:"GET",success:j.templateGetSuccess(a,b),error:j.templateGetError(a,d,b)},k.direct(e)):b(c)},getSync:function(a){var b=this.store.getTemplate(a),c,d,e;if(!b||$.inArray(a,i)!==-1)c=j.getTemplatePath(a),d=null,e={async:!1,url:c,dataType:"html",type:"GET",success:function(a){d=a},error:function(b){$.inArray(a)===-1&&i.push(a),d=h("HTTP Status code: exception.status",a,c)}},$.ajax(e),d===null?d=h("An unknown error occurred.",a,c):(this.store.storeTemplate(a,d),b=this.store.getTemplate(a));return b},infuse:function(a,b){var c=$.extend({},d,b),e=this,f=c.targetSelector||e.config.domTargetResolver(a);e.get(a,function(a){var b=a;c.preRender(f,b),b=e.config.renderInstruction(b,c.model),c.render(f,b),c.postRender(f)})}};a.infuser=l})(window) \ No newline at end of file diff --git a/src/Api.js b/src/Api.js index 5bbd7fd..19cbede 100644 --- a/src/Api.js +++ b/src/Api.js @@ -10,7 +10,8 @@ var infuser = { templateUrl: "", templateSuffix: ".html", templatePrefix: "", - renderInstruction: function(template, model) { return template; } // NO_OP + renderInstruction: function(template, model) { return template; }, // NO_OP + domTargetResolver: function(templateId) { return "#" + templateId } // DEFAULT MAPPING }, get: function(templateId, callback) { @@ -67,15 +68,16 @@ var infuser = { return template; }, - infuse: function(templateId, targetDomElement, renderOptions) { + infuse: function(templateId, renderOptions) { var options = $.extend({}, defaultRenderOptions, renderOptions), - self = this; + self = this, + targetElement = options.targetSelector || self.config.domTargetResolver(templateId); self.get(templateId, function(template) { var _template = template; - options.preRender(targetDomElement, _template); + options.preRender(targetElement, _template); _template = self.config.renderInstruction(_template, options.model); - options.render(targetDomElement, _template); - options.postRender(targetDomElement); + options.render(targetElement, _template); + options.postRender(targetElement); }); } }; \ No newline at end of file