UNPKG

hellojs-xiaotian

Version:

A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)

39 lines (35 loc) 2.21 kB
var attrHtmlToJavascriptMap = { 'class': 'className', 'for': 'htmlFor' }; ko.bindingHandlers['attr'] = { 'update': function(element, valueAccessor, allBindings) { var value = ko.utils.unwrapObservable(valueAccessor()) || {}; ko.utils.objectForEach(value, function(attrName, attrValue) { attrValue = ko.utils.unwrapObservable(attrValue); // To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely // when someProp is a "no value"-like value (strictly null, false, or undefined) // (because the absence of the "checked" attr is how to mark an element as not checked, etc.) var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined); if (toRemove) element.removeAttribute(attrName); // In IE <= 7 and IE8 Quirks Mode, you have to use the Javascript property name instead of the // HTML attribute name for certain attributes. IE8 Standards Mode supports the correct behavior, // but instead of figuring out the mode, we'll just set the attribute through the Javascript // property for IE <= 8. if (ko.utils.ieVersion <= 8 && attrName in attrHtmlToJavascriptMap) { attrName = attrHtmlToJavascriptMap[attrName]; if (toRemove) element.removeAttribute(attrName); else element[attrName] = attrValue; } else if (!toRemove) { element.setAttribute(attrName, attrValue.toString()); } // Treat "name" specially - although you can think of it as an attribute, it also needs // special handling on older versions of IE (https://github.com/SteveSanderson/knockout/pull/333) // Deliberately being case-sensitive here because XHTML would regard "Name" as a different thing // entirely, and there's no strong reason to allow for such casing in HTML. if (attrName === "name") { ko.utils.setElementName(element, toRemove ? "" : attrValue.toString()); } }); } };