Skip to content

Commit a8c8530

Browse files
committed
fixing dom-repeat issue
mqtt-connection.subscriptions is an array of mqtt-subscriptions. It should be possible to bind to that property and iterate over all subscriptions via dom-repeat and then iterate over all messages via dom-repaet. Currently the dom-repeat that iterates over all messages is not getting update/ redrawn when new messages arrive.
1 parent a29d6e2 commit a8c8530

File tree

3 files changed

+92
-47
lines changed

3 files changed

+92
-47
lines changed

demo/elements/demo-element.html

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<link rel="import" href="../../../paper-tooltip/paper-tooltip.html">
1010
<link rel="import" href="../../mqtt-elements.html">
1111

12+
<link rel="import" href="mqtt-subscription-view.html">
13+
14+
1215
<dom-module id="demo-element">
1316

1417
<link rel="import" type="css" href="demo.css">
@@ -28,6 +31,7 @@
2831
<!--create a mqtt subscription for every item in topics-->
2932
<template is="dom-repeat" items="{{topics}}" as="t">
3033
<mqtt-subscription topic="[[t.topic]]"
34+
messages="{{t.messages}}"
3135
number-of-messages="10"
3236
qos="[[t.qos]]"></mqtt-subscription>
3337
</template>
@@ -113,40 +117,8 @@ <h3>Publish on <span>[[topic]]</span></h3>
113117
</div>
114118

115119
<!--output every subscription-->
116-
<template id="subscriptionList" is="dom-repeat" items="[[subscriptions]]" as="sub" observe="messages.*">
117-
<div class="vertical-section">
118-
<div>
119-
<div class="sub-header">
120-
<h2 class="title">Subscribed to - <span>[[sub.topic]]</span></h2>
121-
<h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
122-
<paper-icon-button on-tap="_removeSubscription"
123-
icon="icons:delete"
124-
alt="remove"
125-
title="remove subscription"></paper-icon-button>
126-
</div>
127-
<table>
128-
<thead>
129-
<tr>
130-
<th colspan="3">Messages</th>
131-
</tr>
132-
<tr>
133-
<th>Topic</th>
134-
<th>QoS</th>
135-
<th class="full">Payload</th>
136-
</tr>
137-
</thead>
138-
<tbody>
139-
<template is="dom-repeat" items="{{sub.messages}}" as="message">
140-
<tr>
141-
<td>{{message.topic}}</td>
142-
<td>{{message.message.qos}}</td>
143-
<td class="full">{{message.parsedPayload}}</td>
144-
</tr>
145-
</template>
146-
</tbody>
147-
</table>
148-
</div>
149-
</div>
120+
<template id="subscriptionList" is="dom-repeat" items="[[topics]]" as="sub">
121+
<mqtt-subscription-view model="[[sub]]"></mqtt-subscription-view>
150122
</template>
151123
</div>
152124
</template>
@@ -166,12 +138,9 @@ <h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
166138
type: Object,
167139
},
168140

169-
subscriptions: {
170-
type: Array,
171-
},
172-
173141
newSubTopic: {
174142
type: String,
143+
value: ""
175144
},
176145

177146
subQos: {
@@ -194,7 +163,7 @@ <h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
194163

195164
topic: {
196165
type: String,
197-
value: "foo/bar",
166+
value: "",
198167
},
199168

200169
messages: {
@@ -224,7 +193,17 @@ <h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
224193

225194
clientId: {
226195
type: String,
196+
},
197+
198+
value: {
199+
type: String,
200+
value: "foo/bar"
227201
}
202+
203+
},
204+
205+
listeners: {
206+
"mqtt-subscription-remove-view": "_removeSubscription"
228207
},
229208

230209
observers: [],
@@ -261,7 +240,7 @@ <h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
261240

262241
_addSub: function () {
263242
if(this.newSubTopic.length > 0) {
264-
this.push("topics", {topic: this.newSubTopic, qos: this.subQos});
243+
this.push("topics", {topic: this.newSubTopic, qos: this.subQos, messages: []});
265244
}
266245
},
267246

@@ -291,11 +270,8 @@ <h3 class="qos">QoS: <span>[[sub.qos]]</span></h3>
291270
},
292271

293272
_removeSubscription: function (event) {
294-
// using the index of the subscriptions array only works because we have not changed the order of the
295-
// subscriptions. Otherwise it would be nessesary to have some sort of ID for each subscription to easily map
296-
// the subscription to the model.
297-
var index = this.subscriptions.indexOf(event.model.sub)
298-
this.splice("topics", index, 1);
273+
var key = Polymer.Collection.get(this.topics).getKey(event.detail);
274+
this.splice("topics", key, 1);
299275
},
300276

301277
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<link rel="import" href="../../../polymer/polymer.html">
2+
3+
<link rel="import" href="../../../paper-styles/demo-pages.html">
4+
5+
<dom-module id="mqtt-subscription-view">
6+
7+
<link rel="import" type="css" href="demo.css">
8+
9+
<template>
10+
<style>
11+
:host {
12+
box-sizing: border-box;
13+
}
14+
</style>
15+
16+
<div>lastMessage: <span>[[model.lastMessage]]</span></div>
17+
<div class="vertical-section">
18+
<div>
19+
<div class="sub-header">
20+
<h2 class="title">Subscribed to - <span>[[model.topic]]</span></h2>
21+
<h3 class="qos">QoS: <span>[[model.qos]]</span></h3>
22+
<paper-icon-button on-tap="_removeSubscription"
23+
icon="icons:delete"
24+
alt="remove"
25+
title="remove subscription"></paper-icon-button>
26+
</div>
27+
<table>
28+
<thead>
29+
<tr>
30+
<th colspan="3">Messages</th>
31+
</tr>
32+
<tr>
33+
<th>Topic</th>
34+
<th>QoS</th>
35+
<th class="full">Payload</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
<template is="dom-repeat" items="[[model.messages]]" as="message">
40+
<tr>
41+
<td>[[message.topic]]</td>
42+
<td>[[message.message.qos]]</td>
43+
<td class="full">[[message.parsedPayload]]</td>
44+
</tr>
45+
</template>
46+
</tbody>
47+
</table>
48+
</div>
49+
</div>
50+
51+
</template>
52+
53+
<script>
54+
Polymer({
55+
is: 'mqtt-subscription-view',
56+
57+
properties: {
58+
model: {
59+
type: Object
60+
}
61+
},
62+
63+
_removeSubscription: function (event) {
64+
this.fire("mqtt-subscription-remove-view", this.model);
65+
},
66+
67+
68+
});
69+
</script>
70+
</dom-module>

mqtt-subscription.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@
209209
// don't do anything user has to supply a payloadParseFunction
210210
break;
211211
case "Unknown":
212-
this.payloadParseFunction = function () {
213-
};
212+
this.payloadParseFunction = function () {};
214213
break;
215214
case "String":
216215
this.payloadParseFunction = String.fromCharCode;

0 commit comments

Comments
 (0)