为什么这种聚合物元素的性质决定不确定?

时间:2015-06-09 19:47:30

标签: javascript polymer web-component polymer-1.0

我正在尝试动态地为iron-ajax模板分配属性,但它会解析为未定义。

<dom-module id="products-service">
  <style>
    :host {
      display: none;
    }
  </style>

  <template>
    <iron-ajax id="productsajax"
      auto
      url="http://localhost:3003/api/taxons/products"
      params='{"token":"my-token"}'
      method='GET'
      on-response='productsLoaded'
      handleAs='json'>
    </iron-ajax>
  </template>
</dom-module>

<script>
(function() {
    Polymer({
        is: 'products-service',

        properties: {
            categoryid: {
                type: String,
                notify: true,
                reflectToAttribute: true
            }
        },

        //here I am trying to add and `id` to the `iron-ajax` `params` attribute.
        ready: function() {
            this.$.productsajax.params.id = this.categoryid;
       }
    });
})();
</script>

生成的网址如下所示:

`http://localhost:3003/api/taxons/products?token=my-token&id=undefined`

dom-module&#39; categoryid属性不是undefined,因为我可以看到属性上反映的正确属性值,这意味着它与如何分配属性有关属性。我也在createdattached回调中尝试了此操作,但仍无效。

编辑: 将categoryid实例化为模块时传递给模块:

<products-service products="{{products}}" categoryid="{{categoryid}}"></products-service>

如此处所示,传递给服务的categoryid已经有了一个值。 (图像上的元素名称可能略有不同。我缩短它们以使问题更简洁。)

enter image description here

调用服务的category-products-list看起来像这样

<dom-module id="category-products-list">
  <template>
    <category-products-service products="{{products}}" categoryid="{{categoryid}}"></category-products-service>
    <div>
      <template is="dom-repeat" items="{{products}}">
       <product-card on-cart-tap="handleCart" product="{{item}}">
         <img width="100" height="100">
         <h3>{{item.name}}</h3>
         <h4>{{item.display_price}}</h4>
       </product-card>
     </template>
  </div>
</template>

</dom-module>

<script>
 (function() {
  Polymer({
  is: 'category-products-list',

  properties: {
    categoryid: {
      type: String,
      notify: true,
      reflectToAttribute: true
    }
  },

ready: function() {
   //this is undefined too
   console.log("categoryid in the list module: "+categoryid)
   }
   });
   })();
</script>

3 个答案:

答案 0 :(得分:6)

我认为你在这里遇到了一系列问题,需要稍微重新考虑你的结构。

首先,因为categoryid属性绑定在元素之外,所以它的初始值将是未定义的。基本上,您的元素已创建并附加,所有生命周期方法都会运行,然后然后 categoryid被设置。这也意味着,因为您设置了iron-ajax auto,所以它最初会尝试使用首次提供的信息发出请求。

我建议的更改:

<dom-module id="products-service">
  <style>
    :host {
      display: none;
    }
  </style>

  <template>
    <iron-ajax id="productsajax"
      url="http://localhost:3003/api/taxons/products"
      params='{"token":"my-token"}'
      method='GET'
      on-response='productsLoaded'
      handleAs='json'>
    </iron-ajax>
  </template>
</dom-module>

<script>
(function() {
    Polymer({
        is: 'products-service',

        properties: {
            categoryid: {
                type: String,
                notify: true,
                reflectToAttribute: true
            }
        },

        observers: [
            // Note that this function  will not fire until *all* parameters
            // given have been set to something other than `undefined`
            'attributesReady(categoryid)'
        ],

        attributesReady: function(categoryid) {
            this.$.productsajax.params.id = categoryid;

            // With the removal of `auto` we must initiate the request
            // declaratively, but now we can be assured that all necessary
            // parameters have been set first.
            this.$.productsajax.generateRequest();
        }
    });
})();
</script>

答案 1 :(得分:0)

问题是你如何将categoryid传递给产品服务......这不是产品服务元素本身的问题。如果你做<products-service categoryid="5"></products-service>它会起作用。显然,应用程序的复杂性要高得多,但我创建了一些可以正常工作的简单元素:

的index.html:

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">

<title>My Test</title>

<!-- Load platform support before any code that touches the DOM. -->
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js">    </script>

<link rel="import" href="elements/product-list.html">

</head>

<body>
  <product-list categoryid="5"></product-list>
</body>

</html>

产品list.html:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../elements/product-service.html">


<dom-module id="product-list">
<style>

</style>

<template>
<product-service categoryid="{{categoryid}}"></product-service>
</template>
</dom-module>

<script>
Polymer({
    is: 'product-list'
});
</script>

产品service.html:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">


<dom-module id="product-service">
<style>

</style>

<template>
<iron-ajax id="productsajax"
  auto
  url="http://localhost:3003/api/taxons/products"
  params='{"token":"my-token"}'
  method='GET'
  on-response='productsLoaded'
  handleAs='json'>
</iron-ajax>
</template>
</dom-module>

<script>
Polymer({
    is: 'product-service',
    properties: {
      categoryid: {
              type: String,
              notify: true,
              reflectToAttribute: true
              }
},

//here I am trying to add and `id` to the `iron-ajax` `params` attribute.
ready: function() {
console.log(this.categoryid);
this.$.productsajax.params.id = this.categoryid;
},
productsLoaded: function(e) {
console.log('Response');
}

});
</script>

答案 2 :(得分:0)

Zikes诊断是正确的,这是返回undefined的原因是页面生命周期是在数据加载到页面之前完成的。但是,我无法得到他的答案,为我的解决方案工作。 (我可能在某处出错。)我的api也使用查询字符串来传递数据,而你的api也没有这样做,我相信这个答案会对遇到这种情况的人有所帮助。为了解决这个问题,我在我的财产中添加了一个观察者,我的答案基于https://www.polymer-project.org/1.0/docs/devguide/observers。我用过简单的观察者。我相信Zikes正在使用一个复杂的。

&#13;
&#13;
<iron-ajax id="productsajax" url= {{ajaxUrl}} method='GET'
      on-response='productsLoaded' handleAs='json'>
</iron-ajax>

Polymer({
    is: 'product-service',
    properties: {
            categoryid: {
                type: String,
                notify: true,
                reflectToAttribute: true,
                observer: 'catIdReady'
            },
            ajaxUrl: {
                type: String,
                notify: true
            }
    },
    catIdReady: function (catidnew, catidold) {
        this.set('ajaxUrl', this._getAjaxUrl());
        this.$.productsajax.generateRequest();
        console.log("catidReady!" + catidnew);
    },
    _getAjaxUrl: function () {
        console.log(this.categoryid);
        return 'http://localhost:3003/api/taxons/products?categoryId=' + 
        this.categoryid;
    },
&#13;
&#13;
&#13;