Browse Source

Fix the loading of component, accept also arrays and objects. Closes #1429

pull/1446/head
Artur Arseniev 8 years ago
parent
commit
4d7d70cddc
  1. 23
      src/dom_components/index.js
  2. 12
      src/editor/model/Editor.js
  3. 27
      test/specs/dom_components/index.js

23
src/dom_components/index.js

@ -25,7 +25,7 @@
* @module DomComponents
*/
import { isEmpty } from 'underscore';
import { isEmpty, isString, isObject, isArray } from 'underscore';
module.exports = () => {
var c = {};
@ -301,18 +301,27 @@ module.exports = () => {
* @return {Object} Loaded data
*/
load(data = '') {
const { em } = this;
let result = '';
if (!data && c.stm) {
data = c.em.getCacheLoad();
}
if (data.components) {
try {
result = JSON.parse(data.components);
} catch (err) {}
} else if (data.html) {
result = data.html;
const { components, html } = data;
if (components) {
if (isObject(components) || isArray(components)) {
result = components;
} else {
try {
result = JSON.parse(components);
} catch (err) {
em && em.logError(err);
}
}
} else if (html) {
result = html;
}
const isObj = result && result.constructor === Object;

12
src/editor/model/Editor.js

@ -631,6 +631,18 @@ module.exports = Backbone.Model.extend({
}
},
logInfo(msg, opts) {
this.log(msg, { ...opts, level: 'info' });
},
logWarning(msg, opts) {
this.log(msg, { ...opts, level: 'warning' });
},
logError(msg, opts) {
this.log(msg, { ...opts, level: 'error' });
},
/**
* Set/get data from the HTMLElement
* @param {HTMLElement} el

27
test/specs/dom_components/index.js

@ -98,6 +98,33 @@ describe('DOM Components', () => {
expect(obj.load()).toEqual([{ test: 1 }]);
});
test('Load data with components as a string', () => {
const result = [{}, {}];
expect(
obj.load({
components: '[{}, {}]'
})
).toEqual(result);
});
test('Load data with components as an array', () => {
const result = [{}, {}];
expect(
obj.load({
components: result
})
).toEqual(result);
});
test('Load data with components as an object', () => {
const result = {};
expect(
obj.load({
components: result
})
).toEqual(result);
});
test('Wrapper exists', () => {
expect(obj.getWrapper()).toBeTruthy();
});

Loading…
Cancel
Save