简体   繁体   English

在聚合物中的回调中设置数组项属性值

[英]Setting Array Item Property Value in Callback in Polymer

I have an application that is using Polymer. 我有一个使用Polymer的应用程序。 In this application, I am binding an array of items to the UI. 在此应用程序中,我将一系列项目绑定到UI。 The user can click a button. 用户可以单击一个按钮。 When that button is clicked, a task associated with a third-party library is called. 单击该按钮时,将调用与第三方库关联的任务。 When that task is completed, it returns a status. 该任务完成后,将返回状态。 I need to bind that status to a property of an item in my array. 我需要将该状态绑定到数组中某个项目的属性。 The third-party library allows me to use a callback function. 第三方库允许我使用回调函数。 For that reason, I'll demonstrate my challenge using JavaScript's baked in setTimeout function. 因此,我将使用setTimeout函数中setTimeout JavaScript演示我的挑战。

my-component.html my-component.html

<dom-module id="view-tests">
    <template>
      <table>
        <tbody>
            <template is="dom-repeat" items="{{ items }}" as="item">              
              <tr>
                <td>[[ item.name ]]</td>
                <td><item-status status="[[ item.status ]]"></item-status></td>
              </tr>
            </template>
        </tbody>
      </table>

      <button on-click="bindClick">Bind</button>
    </template>

    <script>
        Polymer({
          is: "my-component",
          properties: {
            items: {
              type: Array,
              notify: true,
              value: function() {
                return [
                  new Item({ name:'Item 1', status:'In Stock' }),
                  new Item({ name:'Item 2', status:'Sold Out' })
                ];
              }  
            },
          },

          bindClick: function() {
            var items = items; 
            setTimeout(function() {
              this.set('items.1.status', 'In Stock');
            }, 1000);             
          }          
        });
    </script>
</dom-module>

As shown in the code snippet above, there is another component item-status . 如上面的代码片段所示,还有另一个组件item-status

item-status.html item-status.html

<dom-module id="test-status">
    <template>
        <span class$="{{ statusClass }}">{{ status }}</span>
    </template>

    <script>
        Polymer({
            is: "item-status",
            properties: {
                status: {
                    type: String,
                    value: '',
                    observer: '_statusChanged'
                }               
            },

            _statusChanged: function(newValue, oldValue) {
                alert(newValue);
                if (newValue === 'In Stock') {
                  this.statusClass = 'green';
                } else if (newValue === 'Sold Out') {
                  this.statusClass = 'red';
                } else {
                  this.statusClass = 'black';
                }
            }
        });
  </script> 
</dom-module>

When a user clicks the "Bind" button, the status does not get updated in the UI. 用户单击“绑定”按钮时,状态不会在UI中更新。 I noticed the alert that I added for debugging purposes appears when the view initially loads. 我注意到最初加载视图时出现了为调试目的添加的alert However, the alert window does not appear when the "Bind" button is clicked. 但是,单击“绑定”按钮时,不会出现alert窗口。 This implies that the observer function is not firing. 这意味着观察者功能未触发。 My callback actually looks something like this: 我的回调实际上看起来像这样:

getStatus(1, function(status) {
  this.set('items.1.status', status);
}); 

How do I set the property of an array item from a callback? 如何通过回调设置数组项的属性?

setTimeout has its own scope. setTimeout有自己的范围。 '.bind(this)' can be used to bind the Polymer element scope to the callback function. '.bind(this)'可用于将Polymer元素范围绑定到回调函数。 Below bindClick function should work 下面的bindClick函数应该可以工作

      bindClick: function() {
        setTimeout(function() {
          this.set('items.1.status', 'In Stock');
        }.bind(this), 1000);
      }          

Working jsbin: http://jsbin.com/mehovu/edit?html,output 工作的jsbin: http ://jsbin.com/mehovu/edit?html,输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM