Skip to content

Commit a1221f4

Browse files
committed
Computed values listen mutations even if property is not defined
1 parent 552767e commit a1221f4

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.26.1",
3+
"version": "0.26.2",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",

src/core/objects/storeMutation.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ dop.core.storeMutation = function(mutation) {
2626

2727

2828
// Collectors
29-
for (;index<total; index++)
30-
if (collectors[index].add(mutation))
31-
return dop.core.runDerivations(path_id);
29+
for (;index<total; index++) {
30+
if (collectors[index].add(mutation)) {
31+
dop.core.runDerivations(path_id_parent);
32+
dop.core.runDerivations(path_id);
33+
return
34+
}
35+
}
3236

3337

3438
var snapshot = new dop.core.snapshot([mutation]);
3539
snapshot.emit();
3640

41+
dop.core.runDerivations(path_id_parent);
3742
dop.core.runDerivations(path_id);
3843
};
3944

test/computeds.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,39 @@ test('Update computeds', function(t) {
644644
// t.equal(collector.mutations.length, 4, "mutations")
645645
collector.emit()
646646
t.end()
647-
});
647+
});
648+
649+
650+
test('Computed must listen mutations even if property is not defined (objects)', function(t) {
651+
var object = dop.register({
652+
items: {},
653+
totalItems: computed(function () {
654+
return Object.keys(this.items).length
655+
})
656+
})
657+
658+
t.equal(object.totalItems, 0)
659+
set(object.items, 'demo', true)
660+
set(object.items, 'demo2', true)
661+
t.equal(object.totalItems, 2)
662+
663+
t.end()
664+
})
665+
666+
667+
test('Computed must listen mutations even if property is not defined (arrays)', function(t) {
668+
var object = dop.register({
669+
items: [],
670+
totalItems: computed(function () {
671+
return this.items.length
672+
})
673+
})
674+
675+
t.equal(object.totalItems, 0)
676+
set(object.items, 0, true)
677+
object.items.push(true)
678+
set(object.items, 'demo2', true)
679+
t.equal(object.totalItems, 2)
680+
681+
t.end()
682+
})

0 commit comments

Comments
 (0)