UniqueID binding in knockout
I'm trying to figure out how to duplicate the functional开发者_如何学City of the Unique Name binding for an element's ID (to get pretty validation working)
ko.bindingHandlers.uniqueID = {
'init': function (element, valueAccessor) {
if (valueAccessor()) {
element.ID = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
}
}
};
The knockout source for uniqueName is
ko.bindingHandlers['uniqueName'] = {
'init': function (element, valueAccessor) {
if (valueAccessor()) {
element.name = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
// Workaround IE 6 issue - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
if (ko.utils.isIe6)
element.mergeAttributes(document.createElement("<input name='" + element.name + "'/>"), false);
}
}
};
ko.bindingHandlers['uniqueName'].currentIndex = 0;
So I think you should have this (note ID should be id, IE6 stuff removed, and uniqueName changed to uniqueId)
ko.bindingHandlers['uniqueId'] = {
'init': function (element, valueAccessor) {
if (valueAccessor()) {
element.id = "ko_unique_" + (++ko.bindingHandlers['uniqueId'].currentIndex);
}
}
};
ko.bindingHandlers['uniqueId'].currentIndex = 0;
精彩评论