Skip to content

Commit

Permalink
dia.Cell: fix toJSON() when defaults() is defined as a method (#1513)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumilingus authored Sep 6, 2021
1 parent 322963b commit 3bbc440
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/dia/Cell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ export const Cell = Backbone.Model.extend({

toJSON: function() {

var defaultAttrs = this.constructor.prototype.defaults.attrs || {};
var attrs = this.attributes.attrs;
var finalAttrs = {};
const defaults = result(this.constructor.prototype, 'defaults');
const defaultAttrs = defaults.attrs || {};
const attrs = this.attributes.attrs;
const finalAttrs = {};

// Loop through all the attributes and
// omit the default attributes as they are implicitly reconstructable by the cell 'type'.
// omit the default attributes as they are implicitly reconstructible by the cell 'type'.
forIn(attrs, function(attr, selector) {

var defaultAttr = defaultAttrs[selector];
const defaultAttr = defaultAttrs[selector];

forIn(attr, function(value, name) {

Expand All @@ -99,7 +100,7 @@ export const Cell = Backbone.Model.extend({
});
});

var attributes = cloneDeep(omit(this.attributes, 'attrs'));
const attributes = cloneDeep(omit(this.attributes, 'attrs'));
attributes.attrs = finalAttrs;

return attributes;
Expand Down
36 changes: 36 additions & 0 deletions test/jointjs/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,41 @@ QUnit.module('cell', function(hooks) {
});
});
});

QUnit.module('toJSON()', function() {

QUnit.test('`defaults` defined as property', function(assert) {

var El = joint.dia.Element.extend({
defaults: {
attrs: { test1: { prop1: true }}
}
});

var el = new El({
id: 'el1',
attrs: { test1: { prop1: true }, test2: { prop2: true }}
});

assert.deepEqual(el.toJSON(), { id: 'el1', attrs: { test2: { prop2: true }}});
});

QUnit.test('`defaults()` defined as function', function(assert) {
var El = joint.dia.Element.extend({
defaults: function() {
return {
attrs: { test1: { prop1: true }}
};
}
});

var el = new El({
id: 'el1',
attrs: { test1: { prop1: true }, test2: { prop2: true }}
});

assert.deepEqual(el.toJSON(), { id: 'el1', attrs: { test2: { prop2: true }}});
});
});
});

0 comments on commit 3bbc440

Please sign in to comment.