当元素绑定到内存中的同一对象时进行绑定 [英] Bindings when elements are bound to same object in memory

查看:80
本文介绍了当元素绑定到内存中的同一对象时进行绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我有多个元素绑定到相同的路径,因此绑定在下面的相同的JS对象.因此,在一个位置对对象的更改将立即由另一元素可见(尽管它不知道).该文档说,我需要对此进行手动提醒或使用自动绑定.因此,正在发生notifyPath调用.

I have a situation where I have multiple elements which are bound to the same path and thus the same JS object underneath. Because of this, changes to the object at one place are immediately visible by the other element (though it doesn't know it). The documentation says I need to manually alert the system about this or use a automatic binding, which I am. So a notifyPath call is happening.

notifyPath DOM遍历中,它最终靠近应该对更改做出反应的元素,但是notifyPath代码确实

In the notifyPath DOM traversal it eventually nears the element which should react to the change, however the notifyPath code does a check to see if the element already knows about the change. In my case, because they are both pointing to the same JS object the old value is already the new value because they're literally the same object in memory. This stops the propagation of the change.

我已经将此问题视为潜在问题,但对解决方法感到好奇同时.

I've submitted this issue as a potential problem but curious about ways around it in the meantime.

这是问题的JSBin: http://jsbin.com/hemexifami/2/edit?html,输出

Here is a JSBin of the issue: http://jsbin.com/hemexifami/2/edit?html,output

这似乎是一种常见的情况.我已经尝试在控制台中直接在元素上手动notifyPath -ing,将其传递给数组,但是代码只是看到oldnew(因为它们都是相同的JavaScript数组,所以它没有)实际上没有做任何事情.文档说它应该返回一个指示,但实际上没有.请参阅我关于此处.

This seems like this kind of thing would be a common case. I've tried manually notifyPath-ing in the console directly on the element, passing it the array, however the code just sees that old is new (since they are both the same JavaScript Array and so it doesn't actually do anything. The documentation says it should return an indication of this but it actually doesn't. See my bug about that here.

你们中有人有打过吗?预先感谢您的帮助!

Have any of you fought this before? Thanks in advance for any help!

推荐答案

聚合物本身并不观察对象引用,而是为了提高性能,它会根据您的命名方式跟踪对象的更改以及它们在树中如何相互关联.

Polymer doesn't observe Object references per se, instead, for performance, it tracks changes to objects based on how you name them and how they relate to each other in the tree.

开发指南中的此部分讨论如何使用array-selector元素在数组中的选择和对象绑定之间进行协调:

The section here in the dev guide discusses how to use the array-selector element to coordinate between selections in arrays and object bindings:

https://www.polymer-project. org/1.0/docs/devguide/templates.html#array-selector

如果我们添加一个知道card.sectionsarray-selector元素,它将为您生成currentSection的可绑定版本.

If we add an array-selector element that knows about card.sections it will produce a bindable version of currentSection for you.

<array-selector id="selector" items="{{card.sections}}" selected="{{currentSection}}"></array-selector>

我们还添加了一些代码来告诉选择器,当索引更改时要选择哪个项目:

We also add a bit of code to tell the selector which item to select when the index changes:

    observers: [
      '_selectSection(card.sections, currentSectionIndex)'
    ],

    _selectSection(sections, index) {
      this.$.sectionSelector.select(sections[index]);
    }        

这是使用数组选择器对jsbin的修改: http://jsbin.com/qidehe/edit?html,输出

Here is a modification of your jsbin using array-selector: http://jsbin.com/qidehe/edit?html,output

这篇关于当元素绑定到内存中的同一对象时进行绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆