在我的测试中,我从主html文件中导入了3个元素:
<html><head><title>my-app</title>
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
<link rel="import" href="elements/app-globals.html">
<link rel="import" href="/Polymer/my-app/elements/my-categories.html">
<link rel="import" href="/Polymer/my-app/elements/my-items.html">
</head>
<body>
<my-categories></my-categories>
<my-items></my-items>
</body>
</html>
使用'app-globals.html':
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="app-globals" attributes="data selectedCategoryId selectedItemId">
<script>
(function() {
var values = {};
Polymer('app-globals', {
ready: function() {
console.log("app-globals -> ready");
this.values = values;
console.dir(this.values);
},
});
})();
</script>
</polymer-element>
'我的-categories.html':
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="/Polymer/my-app/elements/app-globals.html">
<polymer-element name="my-categories">
<template>
<app-globals id="globals"></app-globals>
<div>selectedCategoryId = {{$.globals.values.selectedCategoryId}}</div>
<ul>
<template repeat="{{category in categories}}">
<li class="li-category" data-_id="{{category._id}}" on-tap="{{selectCategory}}">{{category.name}}</li>
</template>
</ul>
</template>
<script>
Polymer('my-categories', {
values: {},
ready: function() {
var HOST = 'xxx.yyy.zzz.www';
var PORT = '8888';
this.categories = <loaded from websocket>;
},
selectCategory: function(event, detail, sender) {
var elt = (event.target.nodeName == 'INPUT')? event.target.parentNode : event.target;
this.values.selectedCategoryId = elt.dataset._id;
this.$.globals.values.selectedCategoryId = elt.dataset._id;
return false;
}
});
</script>
</polymer-element>
当点击其中一个li元素时,'selectedCategoryId'会被更新。
这个值通过全局变量传递给最后一个元素'my-items.html':
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="/Polymer/my-app/elements/app-globals.html">
<polymer-element name="my-items">
<template>
<app-globals id="globals"></app-globals>
<style>
</style>
<div >selectedCategoryId = {{$.globals.values.selectedCategoryId}}</div>
<ul>
<template repeat="{{item in items}}"
on-category-tap="{{handleCategoryTap}}">
<li class="li-item" data-_id="{{item._id}}" on-click="{{selectItem}}">{{item.title}}</li>
</template>
</ul>
</template>
<script>
Polymer('my-items', {
values: {},
ready: function() {
var HOST = 'xxx.yyy.zzz.www';
var PORT = '8888';
console.log("'my-items' -> this.$.globals.values:");
console.dir(this.$.globals.values);
for(var prop in this.$.globals.values) {
if(this.$.globals.values.hasOwnProperty(prop)) {
console.log("this.$.globals.values[" + prop + "] = " + this.$.globals.values[prop] + "");
}
}
console.log("'my-items' -> this.$.globals.values.selectedCategoryId = " + this.$.globals.values.selectedCategoryId);
},
handleCategoryTap: function(event) {...,
selectItem: function(event) {...}
return false;
} });
</script>
给我带来了一些奇怪的东西: console.dir(this。$。globals.values)给出了相应的响应: 对象{ selectedCategoryId:“547dfb6578be56f6630041a8” }
但是,选择此属性“selectedCategoryId”的结果为: console.log(“'my-items' - &gt; this。$。globals.values.selectedCategoryId =”+ this。$。globals.values.selectedCategoryId); 给我: 'my-items' - &gt;这个。$。globals.values.selectedCategoryId = undefined undefined为什么?
答案 0 :(得分:0)
我建议您使用:https://github.com/akc42/akc-meta
我确实使用它通过本地数据绑定在DOM之间共享信息!
如果您想要一个有效的示例,请参阅我的项目:https://github.com/MeTaNoV/firebase-element-extended