codepb/jquery-template

[Feature request] data-href-append

jor77 opened this issue · 6 comments

What about having a data-href-append binding?

I have a lot of links like to dynamic pages where I need to pass the ID for a product or a client or something else... In such case I always have to write a data formatter...

Having data-href-append I could write the href of the element like "www.examplesite.com/client.php?id=" and have the parameter I want appended to this, saving me the trouble to write a data formatter :-)

@jor77
Can you explain how you are achieving that using data formatter?
Thanks

Sure..

Before calling the template I add a data formatter like that:

$.addTemplateFormatter({
picLink: function (value) {
return "clients/img/" + value + ".jpg";
},
productPageLink: function (value) {
return "product.php?ID=" + value;
}

Then you use the data formatter in the template (as explained in the documentation)

OK Thanks

I think a data-href-append will add more complexity when a formatter does this nicely.

Also to save you some effort with your formatters, have a look at the simple example provided as part of the solution:
https://github.com/codepb/jquery-template/blob/master/Examples/SimpleExample/example.html

In here there is the following code:
$.addTemplateFormatter({
upperCaseFormatter: function (value, options) {
return value.toUpperCase();
},
lowerCaseFormatter: function (value, options) {
return value.toLowerCase();
},
sameCaseFormatter: function (value, options) {
if (options == "upper") {
return value.toUpperCase();
} else {
return value.toLowerCase();
}
}
});

As you can see, the sameCaseFormatter does the job of both the previous two formatters, but utilising the options parameter. You can assign values to this by using the "data-format-options" attribute. Therefore you could use one formatter to construct a url with an id, and pass in the url using data-format options.

Great!
I really didn't thought about this approach! It work like a charm 👍

I will close this issue, as I will not be implementing the feature requested.