Q:

Callback when list is edited in CoffeeScript/JS

I would like to be able to add and delete items from an array in javascript.   When an item is added to the array, I would like another function to be notified about what item was added.   When an item is deleted, I would like a function to be notified what was deleted. 
I am using CoffeeScript, but an answer or point in a direction in CoffeeScript or JS would be very helpful. 

A:

watchMe = []

doPush = (value)->
  # do whatever

watchMe.push = ->
  doPush arguments[0]
  Array.prototype.push.apply @, arguments

# this will call doPush with 'foo', and add 'foo' to the watchMe array
watchMe.push 'foo'

You can redefine native Array.prototype methods on your array, calling your custom function and then the native javascript parent function.  
The example above shows how to override adding elements using push().  You would do the same for removing elements using pop(), splice(), etc.  
Check out W3schools for a detailed listing of array methods that you can overload using the method above.  
Here's a link to a more detailed coffeescript demo of the above.  You can test locally or copy and paste that over at coffeescript.org

