Skip to content

Commit

Permalink
Added the domTargetResolver function to the infuser config, and updat…
Browse files Browse the repository at this point in the history
…ed examples with new dom target approach
  • Loading branch information
ifandelse committed Oct 29, 2011
1 parent 9041dea commit d6a9c13
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/shorthand/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ $(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(){
// Shorthand syntax also allows you to specify preRender and postRender callbacks, as well as a render override
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'); },
Expand Down
2 changes: 1 addition & 1 deletion examples/trafficcop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 8 additions & 6 deletions lib/infuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
};
Expand Down
Binary file modified lib/infuser.min.gz.js
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/infuser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
};

0 comments on commit d6a9c13

Please sign in to comment.