问题
I'm writing a wrapper around a js library with Polymer components. Basically, I need to be able to assign attributes to a component, and forward them to an instance of a js object.
The problem is that Polymer (or webcomponents in general?) forces attribute names to be lowercase.
Declaring the element
<some-element fooBar="baz"></some-element>
Generic change listener
attributeChanged: function(attrName, oldVal, newVal){
// attrName -> foobar, which is not a member of someInstance
this.someInstance[attrName] = newVal;
}
Is there some way to get the camel-cased name? Maybe I can create a hash from the publish
object on the prototype... but how do I reference that?
回答1:
Aha! publish
is actually available on the component instance! This allows me to grab the names and make a map from lowercase to camelcase.
var map = {};
Object.keys(this.publish).forEach(function(key){
map[key.toLowerCase()] = key;
});
来源:https://stackoverflow.com/questions/29178675/polymer-camelcase-attributes