melanke / Watch.JS

watch the changes of any object or attribute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Observe Array Index

rummelj opened this issue · comments

Hi! Thanks for the great library. Is it somehow possible to observe a certain index of an Array?

Example:
var arr = ["foo", "bar", "baz"]
WatchJS.watch(arr, 1, callback)

Expected Behaviour:
arr[0] = "newFoo" // callback is NOT called
arr[1] = "newBar" // callback IS called
arr[2] = "newBaz" // callback is NOT called

Current Behaviour:
Callback is never called

Best
Johannes

commented

Hi,

Perhaps I over simplified the problem but shouldn't it be:
WatchJS.watch(arr[1], callback) ?
Seems to work for me :-)

Erik

WatchJS.watch(arr[1], callback) will not work.
but it will work if you change the array like this:

var index = 2;
arr.splice(index, 1, 'newValue');
// 1 is for deleting one item at that position before adding a new to the same position