mrmartineau
12/12/2013 - 11:07 AM

String.substitute and example used on the uk.thebar.com

String.substitute and example used on the uk.thebar.com

var query = this.options.queryUrl.substitute({
	spirits: this.element.find('.ingredients-list-spirits .is-selected').map(function() {
		return $(this).data('value');
	}).get().join(),
	mixers: this.element.find('.ingredients-list-mixers .is-selected').map(function() {
		return $(this).data('value');
	}).get().join(),
	garnishes: this.element.find('.ingredients-list-garnishes .is-selected').map(function() {
		return $(this).data('value');
	}).get().join()
});
$(this).attr('href',query.substitute({ type : $(this).attr('data-type') }));
String.prototype.substitute = function (object) {
	return this.replace(/\{(.+?)\}/g, function (match, name) {
		return name in object ? object[name] : match;
	});
};