Browse Source

Avoid multiple attributes changes

pull/2295/head
Artur Arseniev 7 years ago
parent
commit
b1dae5c443
  1. 11
      src/dom_components/model/Component.js
  2. 6
      test/specs/dom_components/model/Component.js

11
src/dom_components/model/Component.js

@ -327,7 +327,11 @@ const Component = Backbone.Model.extend(Styleable).extend(
* @private
*/
attrUpdated(m, v, opts = {}) {
this.setAttributes(this.get('attributes'), { ...opts, silent: 1 });
const attrPrev = { ...this.previous('attributes') };
const diff = shallowDiff(attrPrev, this.get('attributes'));
keys(diff).forEach(pr =>
this.trigger(`change:attributes:${pr}`, this, diff[pr], opts)
);
},
/**
@ -351,11 +355,6 @@ const Component = Backbone.Model.extend(Styleable).extend(
delete attrs.style;
this.set('attributes', attrs, opts);
const attrPrev = { ...this.previous('attributes') };
const diff = shallowDiff(attrPrev, attrs);
keys(diff).forEach(pr =>
this.trigger(`change:attributes:${pr}`, this, diff[pr], opts)
);
return this;
},

6
test/specs/dom_components/model/Component.js

@ -281,10 +281,10 @@ module.exports = {
});
});
test.only('setAttributes overwrites correctly', () => {
test('setAttributes overwrites correctly', () => {
obj.setAttributes({ id: 'test', 'data-test': 'value', a: 'b' });
// obj.setAttributes({ 'data-test': 'value2' });
// expect(obj.getAttributes()).toEqual({ 'data-test': 'value2' });
obj.setAttributes({ 'data-test': 'value2' });
expect(obj.getAttributes()).toEqual({ 'data-test': 'value2' });
});
test('append() returns always an array', () => {

Loading…
Cancel
Save