|
//Enhanced version string template from https://github.com/faulknercs/Knockstrap/blob/master/src/templates/templatesWrapper.js |
|
define(['knockout'], function (ko) { |
|
|
|
//string template engine |
|
|
|
//define a template source that tries to key into an object to find a template string |
|
var data = {}, |
|
engine; |
|
|
|
ko.templates = {}; |
|
|
|
ko.templateSources.stringTemplate = function (template) { |
|
this.templateName = template; |
|
}; |
|
|
|
ko.utils.extend(ko.templateSources.stringTemplate.prototype, { |
|
data: function (key, value) { |
|
data[this.templateName] = data[this.templateName] || {}; |
|
|
|
if (arguments.length === 1) { |
|
return data[this.templateName][key]; |
|
} |
|
|
|
data[this.templateName][key] = value; |
|
}, |
|
text: function (value) { |
|
if (arguments.length === 0) { |
|
return ko.templates[this.templateName]; |
|
} |
|
|
|
ko.templates[this.templateName] = value; |
|
} |
|
}); |
|
|
|
engine = function () { |
|
this.allowTemplateRewriting = false; |
|
}; |
|
|
|
|
|
engine.prototype = ko.nativeTemplateEngine.prototype; |
|
engine.prototype.constructor = engine; |
|
|
|
engine.prototype.makeTemplateSource = function (template, doc) { |
|
var elem; |
|
if (typeof template === "string") { |
|
elem = (doc || document).getElementById(template); |
|
|
|
if (elem) { |
|
return new ko.templateSources.domElement(elem); |
|
} |
|
|
|
return new ko.templateSources.stringTemplate(template); |
|
} else if (template && (template.nodeType === 1) || (template.nodeType === 8)) { |
|
return new ko.templateSources.anonymousTemplate(template); |
|
} |
|
}; |
|
|
|
engine.prototype.getTemplate = function (name) { |
|
return ko.templates[name]; |
|
}; |
|
|
|
|
|
engine.prototype.addTemplate = function (name, template) { |
|
if (arguments.length < 2) { |
|
throw new Error('template is not provided'); |
|
} |
|
|
|
ko.templates[name] = template; |
|
}; |
|
|
|
engine.prototype.removeTemplate = function (name) { |
|
if (!name) { |
|
throw new Error('template name is not provided'); |
|
} |
|
|
|
delete ko.templates[name]; |
|
}; |
|
|
|
engine.prototype.isTemplateExist = function (name) { |
|
return !!ko.templates[name]; |
|
}; |
|
|
|
ko.marketplaceEngine = new engine(); |
|
}); |