From 578e202cfbbc868e1bd0d54ecd249f11330f7377 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 23 Feb 2021 23:11:37 +0100 Subject: [PATCH] Always recover serialized symbols --- src/dom_components/model/Component.js | 31 +++++++++++++++++++--- test/specs/dom_components/model/Symbols.js | 22 +++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 3592e3905..a2c2df471 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -628,20 +628,45 @@ const Component = Backbone.Model.extend(Styleable).extend( ); }, + __getAllById() { + const { em } = this; + return em ? em.get('DomComponents').allById() : {}; + }, + __getSymbol() { - return this.get(keySymbol); + let symb = this.get(keySymbol); + if (symb && isString(symb)) { + const ref = this.__getAllById()[symb]; + if (ref) { + symb = ref; + this.set(keySymbol, ref); + } else { + symb = 0; + } + } + return symb; }, __getSymbols() { - return this.get(keySymbols); + let symbs = this.get(keySymbols); + if (symbs && isArray(symbs)) { + symbs.forEach((symb, idx) => { + if (symb && isString(symb)) { + symbs[idx] = this.__getAllById()[symb]; + } + }); + symbs = symbs.filter(symb => symb && !isString(symb)); + } + return symbs; }, __getSymbToUp(opts = {}) { const { em } = this; const symbEnabled = em && em.get('symbols'); const { fromInstance } = opts; - const symbols = this.get(keySymbols) || []; + const symbols = this.__getSymbols() || []; const symbol = this.__getSymbol(); + !symbols.filter && console.log('!symbols.filter', symbols); let result = symbol && !fromInstance ? [symbol] diff --git a/test/specs/dom_components/model/Symbols.js b/test/specs/dom_components/model/Symbols.js index ca6d9195c..28a727d7a 100644 --- a/test/specs/dom_components/model/Symbols.js +++ b/test/specs/dom_components/model/Symbols.js @@ -98,6 +98,28 @@ describe('Symbols', () => { expect(jsonSymb[keySymbols]).toEqual([idComp]); }); + test('Serialized symbol references are always recovered', () => { + const comp = wrapper.append(simpleComp)[0]; + const symbol = createSymbol(comp); + const idComp = comp.getId(); + const idSymb = symbol.getId(); + // Serialize symbols + comp.set(keySymbol, idSymb); + symbol.set(keySymbols, [idComp]); + // Check updates from instance + const newAttr = { class: 'test', myattr: 'myvalue' }; + comp.setAttributes(newAttr); + comp.components('New text content'); + expect(symbol.getAttributes()).toEqual(newAttr); + expect(symbol.toHTML()).toBe(comp.toHTML()); + // Check updates from symbol + const newAttr2 = { class: 'test2', myattr2: 'myvalue2' }; + symbol.setAttributes(newAttr2); + symbol.components('New text content2'); + expect(comp.getAttributes()).toEqual(newAttr2); + expect(symbol.toHTML()).toBe(comp.toHTML()); + }); + test("Removing one instance doesn't affect others", () => { const comp = wrapper.append(simpleComp)[0]; const symbol = createSymbol(comp);