我在循环两个聚合物 firebase-collection 元素时遇到问题。
使用我的数据库结构,我首先必须检查用户有权访问哪些事件,然后从事件中获取有关该事件的信息。
这段代码的问题是,当我循环第二个 firebase-collection 时,“事件”上的数据绑定在所有重复的事件上都是相同的,因此每个 h4 上的名称都相同。
那么有没有办法在 data="{{ }}" 中拥有唯一的变量。
或者有更好的方法来写出数据吗?
<firebase-collection data="{{userData}}" location="{{_getCorrectUrl()}}"></firebase-collection>
<template is="dom-repeat" items="{{userData}}" as="user">
<firebase-collection data="{{events}}" location="{{_getCorrectEventsUrl(user.__firebaseKey__)}}" ></firebase-collection>
<template is="dom-repeat" items="{{events}}" as="event">
<h4>{{event.value.name}}</h4>
</template>
</template>
我有一个解决方案,我认为这不是解决这个问题的最佳方法,但它对我有用。我创建了另一个名为“my-user”的元素,我公开了一个名为“user”的属性,该属性从第一个 dom-repeat 中分配了 user 的值。一切正常,但是,我对这个解决方案不满意。我仍在寻找一种不涉及为此创建专用元素的解决方案。
<firebase-collection data="{{userData}}" location="{{_getCorrectUrl()}}">
</firebase-collection>
<template is="dom-repeat" items="{{userData}}" as="user">
<my-user user="[[user]]"></my-user>
</template>
和另一个“我的用户”:
<dom-module id="my-user">
<template>
<firebase-collection data="{{events}}" location="{{_getCorrectEventsUrl(user)}}" ></firebase-collection>
<template is="dom-repeat" items="{{events}}" as="event">
<h4>{{event.value.name}}</h4>
</template>
</template>
<script>
(function () {
Polymer({
is: 'my-user',
_getCorrectEventsUrl: function(obj) {
return 'https://app.firebaseio.com/users/' + obj.__firebaseKey__;
},
properties: {
user:{
type:Object,
},
},
});
})();
</script>
</dom-module>
EDIT
现在,在回答这个问题一段时间后,我开始意识到,当有人使用 NoSQL 数据库遇到这种情况时,通常会凸显出设计中的问题。数据应该是非规范化的,并且应该避免像 SQL 数据库中通常所做的那样进行连接。观看该在YouTube上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)