4

谁能给我一些关于聚合物、x-tag 和 vanilla js 之间区别的想法?

我在我的项目中使用了聚合物,但我想比较聚合物、x-tag 和 vanilla js。

4

2 回答 2

5

Web Components is the native implementation in the browsers. Polymer is a library that act as a very thin layer on top of the Web Components technologies. X-Tag is another library that is even thinner because it only relies on one of the four Web Components technologies.

I've written an article about that: http://pascalprecht.github.io/2014/07/21/polymer-vs-x-tag-here-is-the-difference/

于 2014-08-04T09:56:10.560 回答
5

VanillaJS 仅意味着在纯 JS 中使用没有任何框架/包装器的 web 组件。

您必须注册您的自定义元素,删除元素并自己处理数据绑定。

两者都x-tag提供Polymer了一个方便和固执己见的 web 组件包装器,大大减少了样板代码。

恕我直言,该Polymer库提供了最方便的方法(关于数据绑定、定义模板等)

这是使用 x-tag 的样子:

xtag.register('x-accordion', {
  // extend existing elements
  extends: 'div',
  lifecycle:{
    created: function(){
      // fired once at the time a component
      // is initially created or parsed
    },
    inserted: function(){
      // fired each time a component
      // is inserted into the DOM
    },
    removed: function(){
      // fired each time an element
      // is removed from DOM
    },
    attributeChanged: function(){
      // fired when attributes are set
    }
  },
  events: {
    'click:delegate(x-toggler)': function(){
      // activate a clicked toggler
    }
  },
  accessors: {
    'togglers': {
      get: function(){
        // return all toggler children
      },
      set: function(value){
        // set the toggler children
      }
    }
  },
  methods: {
    nextToggler: function(){
      // activate the next toggler
    },
    previousToggler: function(){
      // activate the previous toggler
    }
  }
});

这就是 Polymer 的样子:

<polymer-element name="polymer-accordion" extends="div" on-click="{{toggle}}">
  <template>
    <!-- shadow DOM here -->
  </template>
  <script>
    Polymer('polymer-accordion' {
        created: function() { ... },
        ready: function() { ... },
        attached: function () { ... },
        domReady: function() { ... },
        detached: function() { ... },
        attributeChanged: function(attrName, oldVal, newVal) {
        },
        toggle : function() {....},
        get togglers() {},
        set togglers(value) {},
        nextToggler : function() {},
        previousToggler : function() {},
   });
  </script>
</polymer-element>
于 2014-07-01T09:15:39.383 回答