googlearchive / observe-js

A library for observing Arrays, Objects and PathValues
1.35k stars 118 forks source link

invalidPath returned when using number-like keys for adding a path observer #86

Closed jjwill closed 9 years ago

jjwill commented 9 years ago

I am running into an issue where this.path_ is always undefined for a PathObserver where the key is a number-like string. The transition here, https://github.com/Polymer/observe-js/blob/0.4.0/src/observe.js#L286, always returns 'error' because the type var above is 'number'.

Changing obj will never trigger the changeFn:

var obj = {'1': 'one'};
var observer = new PathObserver(obj, '1');  
observer.open(changeFn, context); // some callback and scope
OlsonDev commented 9 years ago

I don't think your path is technically supported. However, here's a workaround:

let obj = { '1': 'one' };
let observer = new PathObserver(obj, '[1]');
observer.open(console.log.bind(console));
obj[1] = 'two'; // console logs: two one PathObserver{ ... }
jjwill commented 9 years ago

Ahh, good call; thanks for the workaround @OlsonDev.