Skip to content

Commit

Permalink
fix: Allow to move sandbox between arrays with existence binder
Browse files Browse the repository at this point in the history
  • Loading branch information
finom committed Apr 2, 2017
1 parent 35b403f commit 3b030b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/array/_processrendering/renderitemnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function renderItemNode({
// moving sandbox does not fire "render" event but it fire "afterrender"
// since "afterrender" means "node is inserted to DOM"
return {
node,
node: node.__matreshkaReplacedByNode || node,
itemEventOptions: {
node,
self: item,
Expand Down Expand Up @@ -106,8 +106,8 @@ export default function renderItemNode({
throw matreshkaError('array:rendered_number_nodes', { length: parsed.length });
}

let node = renderedInArrays[selfId] = parsed[0];
node = node.__matreshkaReplacedByNode || node;
const node = renderedInArrays[selfId] = parsed[0];


if (bindRenderedAsSandbox) {
if (forceRerender) {
Expand Down Expand Up @@ -137,8 +137,8 @@ export default function renderItemNode({

triggerOne(item, 'render', itemEventOptions);

return { node, itemEventOptions };
return { node: node.__matreshkaReplacedByNode || node, itemEventOptions };
}

return { node };
return { node: node.__matreshkaReplacedByNode || node };
}
39 changes: 39 additions & 0 deletions test/spec/bindings/existence_binder_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,43 @@ describe('Existence binder', () => {
Array.from(arr.nodes.sandbox.childNodes).map(({ nodeName }) => nodeName)
).toEqual(['#comment', 'DIV', 'DIV', '#comment', 'DIV']);
});

it('allows to move sandbox across arrays', () => {
class Arr extends MatreshkaArray {
constructor(...args) {
super(...args)
.bindNode('sandbox', '<div data-foo="bar"></div>');
}
}

const arr = new Arr();
const arr2 = new Arr();
const obj = { exists: true }; // eslint-disable-line no-shadow
arr.itemRenderer = arr2.itemRenderer = '<div><span></span></div>';

arr.push(obj);
const arrItemNode = arr.nodes.sandbox.childNodes[0];
expect(arrItemNode.nodeName).toEqual('DIV');

bindNode(obj, 'exists', ':sandbox', existence(), noDebounceFlag);
obj.exists = false;

const replacedBy = arr.nodes.sandbox.childNodes[0];

expect(replacedBy.nodeName).toEqual('#comment');

arr2.push_(obj, {
moveSandbox: true
});

expect(
arr2.nodes.sandbox.childNodes[0]
).toEqual(replacedBy);

obj.exists = true;

expect(
arr2.nodes.sandbox.childNodes[0]
).toEqual(arrItemNode);
});
});

0 comments on commit 3b030b3

Please sign in to comment.