Created
July 16, 2014 17:19
-
-
Save andrew-aladev/8e2bf5d979544f37855e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($, undefined) { | |
"use strict"; | |
if (!ko.templates) { | |
ko.templates = {}; | |
} | |
var StringTemplateSource = function(string) { | |
this.string = string; | |
} | |
StringTemplateSource.prototype.data = function (key, value) { | |
var data = this.string.template_data; | |
if (!data) { | |
this.string.template_data = data = {}; | |
} | |
if (arguments.length == 1) { | |
return data[key]; | |
} | |
data[key] = value; | |
} | |
StringTemplateSource.prototype.text = function (string) { | |
if (arguments.length == 0) { | |
return this.string; | |
} | |
string.template_data = this.string.template_data; | |
this.string = string; | |
} | |
var Engine = ko.nativeTemplateEngine.clone(); | |
Engine.prototype.makeTemplateSource = function (template_key, options, template_document, callback) { | |
if (typeof template_key == "string") { | |
var template = ko.templates[template_key]; | |
if (template) { | |
if (typeof template == "string") { | |
callback(new StringTemplateSource(template)); | |
} else if (typeof template == "object" && (template.nodeType == 1 || template.nodeType == 8)) { | |
callback(new ko.templateSources.anonymousTemplate(template)); | |
} else { | |
throw new Error("Unknown template : " + template); | |
} | |
} else { | |
if (typeof options == "object" && options.url) { | |
$(this).one("loaded_url:" + options.url, function () { | |
template = ko.templates[template_key]; | |
if (template) { | |
callback(new StringTemplateSource(template)); | |
} else { | |
throw new Error("Can't load template key : " + template_key); | |
} | |
}); | |
if (!this.loading_urls) { | |
this.loading_urls = {}; | |
} | |
if (this.loading_urls[options.url]) { | |
return; | |
} | |
this.loading_urls[options.url] = true; | |
var self = this; | |
$.get("/combres.axd/" + options.url + "/1", function (template_data) { | |
$(template_data).filter("script").each(function() { | |
ko.templates[$(this).attr("id")] = $(this).html(); | |
}); | |
self.loading_urls[options.url] = true; | |
$(self).trigger("loaded_url:" + options.url); | |
}); | |
} else { | |
var template = $("script[id='" + template_key + "']").html(); | |
if (template) { | |
ko.templates[template_key] = template; | |
callback(new StringTemplateSource(template)); | |
} else { | |
throw new Error("Can't load template key : " + template_key); | |
} | |
} | |
} | |
} else if (typeof template_key == "object" && (template_key.nodeType == 1 || template_key.nodeType == 8)) { | |
callback(new ko.templateSources.anonymousTemplate(template_key)); | |
} else { | |
throw new Error("Unknown template key : " + template_key); | |
} | |
} | |
Engine.prototype.renderTemplate = function (template, binding_context, options, template_document, callback) { | |
var self = this; | |
this.makeTemplateSource(template, options, template_document, function (template_source) { | |
self.renderTemplateSource(template_source, binding_context, options, callback); | |
}); | |
} | |
Engine.prototype.renderTemplateSource = function (template_source, binding_context, options, callback) { | |
if (template_source.nodes) { | |
callback(ko.utils.makeArray(template_source.nodes().childNodes)); | |
} else { | |
callback(ko.utils.parseHtmlFragment(template_source.text())); | |
} | |
} | |
ko.setTemplateEngine(new Engine()); | |
}) (jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers.scroll_wrapper = { | |
init : function (element) { | |
return { controlsDescendantBindings : true }; | |
}, | |
update : function (element, value_accessor, all_bindings_accessor, model) { | |
var scroll = $(element).data("scroll"); | |
if (scroll) { | |
return; | |
} | |
var content_name = "scroll_wrapper_" + scroll_counter++; | |
var children = $(element).children(); | |
var container = $("<div></div>").appendTo($(element).parent()).get(0); | |
children.appendTo(container); | |
ko.utils.domData.set(container, "anonymous_data", {containerData : container}); | |
ko.templates[content_name] = container; | |
var options = ko.utils.unwrapObservable(value_accessor()); | |
var scroll = new Scroll(element, options, content_name, model); | |
$(element).data("scroll", scroll); | |
scrolls.push(scroll); | |
scroll.render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment