I am using Knockout JS to create an editor. I am using the foreach property to loop around a list in my model.
-
The uniqueName binding just increments an index and sets the name (with a fix for IE).
It looks like:
ko.bindingHandlers['uniqueName'] = {
'init': function (element, valueAccessor) {
if (valueAccessor()) {
element.name = "ko_unique_" + (++ko.bindingHandlers['uniqueName'].currentIndex);
// Workaround IE 6/7 issue
// - https://github.com/SteveSanderson/knockout/issues/197
// - http://www.matts411.com/post/setting_the_name_attribute_in_ie_dom/
if (ko.utils.isIe6 || ko.utils.isIe7)
element.mergeAttributes(document.createElement(""), false);
}
}
};
So, you can create a custom binding that uses the last index and sets the appropriate attribute
ko.bindingHandlers.valmsg = {
init: function(element) {
element.setAttribute("data-valmsg-for", "ko_unique_" + ko.bindingHandlers.uniqueName.currentIndex);
}
};
Now, you would just use it like:
- 热议问题