Skip to content

Commit

Permalink
bugfix(ArrayObserver): ractivejs#3343 - set this of callback using ra…
Browse files Browse the repository at this point in the history
…ctive
  • Loading branch information
marcalexiei committed Sep 7, 2020
1 parent 1cb46e9 commit 9ff62ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Ractive/prototype/observe/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class ArrayObserver {

dispatch(): void {
try {
this.callback(this.pending);
this.callback.call(this.ractive, this.pending);
} catch (err) {
warnIfDebug(
`Failed to execute array observer callback for '${this.keypath}': ${err.message || err}`
Expand Down
23 changes: 23 additions & 0 deletions tests/browser/methods/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,29 @@ export default function() {
r.push('list', 1);
});

test('array observers callback must have ractive instance as this (#3343)', t => {
t.expect(2);
let thisInsideObserveInitProperty;

const r = new Ractive({
data: { list: [] },
observe: {
list: {
handler() {
thisInsideObserveInitProperty = this;
},
array: true
}
}
});

r.observe('list', function() {
t.equal(this, r);
});

t.equal(r, thisInsideObserveInitProperty);
});

test(`plain observers allow a hook to set the 'old' value`, t => {
t.expect(4);

Expand Down

0 comments on commit 9ff62ff

Please sign in to comment.