@@ -26,11 +26,6 @@ export default {
2626 props: { type: Object , default : () => ({}) },
2727 state: { type: Object , default : () => ({}) }
2828 },
29- data () {
30- return {
31- selection: null
32- }
33- },
3429 computed: {
3530 ... mapState (' data' , [' messages' ]),
3631 selectedColor : function () {
@@ -59,63 +54,43 @@ export default {
5954 })
6055 }
6156 return options
57+ },
58+ selection: {
59+ get () {
60+ const msg = this .messages [this .id ]
61+ let selection = null
62+ if (msg) {
63+ if (Array .isArray (msg .payload ) && msg .payload .length === 0 ) {
64+ selection = null
65+ } else if (this .findOptionByValue (msg .payload ) !== null ) {
66+ selection = msg .payload
67+ }
68+ }
69+ return selection
70+ },
71+ set (value) {
72+ if (! this .messages [this .id ]) {
73+ this .messages [this .id ] = {}
74+ }
75+ this .messages [this .id ].payload = value
76+ }
6277 }
6378 },
6479 created () {
6580 // can't do this in setup as we are using custom onInput function that needs access to 'this'
66- this .$dataTracker (this .id , this . onInput , this . onLoad , this .onDynamicProperty , this . onSync )
81+ this .$dataTracker (this .id , null , null , this .onDynamicProperty , null )
6782
6883 // let Node-RED know that this widget has loaded
6984 this .$socket .emit (' widget-load' , this .id )
7085 },
7186 methods: {
72- onInput (msg ) {
73- // update our vuex store with the value retrieved from Node-RED
74- this .$store .commit (' data/bind' , {
75- widgetId: this .id ,
76- msg
77- })
78-
79- // make sure our v-model is updated to reflect the value from Node-RED
80- if (msg .payload !== undefined ) {
81- if (Array .isArray (msg .payload ) && msg .payload .length === 0 ) {
82- this .selection = null
83- } else {
84- if (this .findOptionByValue (msg .payload ) !== null ) {
85- this .selection = msg .payload
86- }
87- }
88- }
89- },
90- onLoad (msg ) {
91- if (msg) {
92- // update vuex store to reflect server-state
93- this .$store .commit (' data/bind' , {
94- widgetId: this .id ,
95- msg
96- })
97- // make sure we've got the relevant option selected on load of the page
98- if (msg .payload !== undefined ) {
99- if (Array .isArray (msg .payload ) && msg .payload .length === 0 ) {
100- this .selection = null
101- } else {
102- if (this .findOptionByValue (msg .payload ) !== null ) {
103- this .selection = msg .payload
104- }
105- }
106- }
107- }
108- },
10987 onDynamicProperty (msg ) {
11088 const updates = msg .ui_update
11189 if (updates) {
11290 this .updateDynamicProperty (' label' , updates .label )
11391 this .updateDynamicProperty (' options' , updates .options )
11492 }
11593 },
116- onSync (msg ) {
117- this .selection = msg .payload
118- },
11994 onChange (value ) {
12095 if (value !== null && typeof value !== ' undefined' ) {
12196 // Tell Node-RED a new value has been selected
0 commit comments