var Binding = {
Value: function (sel) {
this.selector = sel;
},
Attribute: function (sel, attr) {
this.selector = sel;
this.attribute = attr;
}
};
(function ($) {
$.fn.postTemplate = function (url, data, template) {
$.each(this, function (nodeix, node) {
var target = node;
$.ajax({
url: url,
data: data,
type: 'POST',
cache: false,
dataType: 'json',
success: function (data) {
if (data.Success) {
var t = new template();
$(target).html(t.render(data));
if (t.OnRender) {
t.OnRender($(target));
}
}
else {
$(target).html(data.Message);
}
}
});
});
};
$.fn.bindTemplate = function (url, bindings, template, targetSelector, event) {
function change(node) {
data = {};
$.each(bindings, function (bindingix, binding) {
var val = binding;
if (binding instanceof Binding.Value) {
val = $(node).find(binding.selector).val();
}
if (binding instanceof Binding.Attribute) {
val = $(node).find(binding.selector).attr(binding.attribute);
}
data[bindingix] = val;
});
$(targetSelector).postTemplate(url, data, template);
}
event = event? event: "change";
$.each(this, function (nodeix, node) {
$.each(bindings, function (bindingix, binding) {
if (event == change && binding instanceof Binding.Value) {
$(binding.selector, node).bind(event, function () {
change(node);
});
}
else {
$(node).bind(event, function () {
change(node);
});
}
});
if (event == "change") {
change(node);
}
});
};
})(jQuery);