聚合物与x-tag和vanilla js之间的比较

Nir*_*iya 4 javascript x-tag polymer

有人能让我对聚合物,x标签和香草js之间的区别有所了解吗?

我在我的项目中使用过聚合物,但我想要比较聚合物,x-tag和vanilla js.

Ümi*_*mit 5

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

您必须注册自定义元素,标记元素并自行处理数据绑定.

双方x-tagPolymer提供围绕网络组件的方便和自以为是的包装,大大减少样板代码.

恕我直言,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
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

这就是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>
Run Code Online (Sandbox Code Playgroud)


Pas*_*cht 5

Web Components是浏览器中的本机实现.Polymer是一个库,它作为Web Components技术之上的一个非常薄的层.X-Tag是另一个更薄的库,因为它只依赖于四种Web组件技术中的一种.

我写过一篇关于此的文章:http://pascalprecht.github.io/2014/07/21/polymer-vs-x-tag-here-is-the-difference/