Polymer camelCase attributes

风格不统一 提交于 2019-12-24 20:49:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!