Advertisement
Serafim

Untitled

Oct 4th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class fn
  2.   @id         = 0
  3.   @observable = []
  4.  
  5.   @watch: (val = null) ->
  6.     value = @observable[@id++]
  7.    
  8.     class getSet
  9.       constructor: (val) ->
  10.         value or=
  11.           value:  undefined
  12.           before: []
  13.           after:  []
  14.          
  15.         return if val?
  16.           fn value.value, val for fn in value.before
  17.           value.value = val
  18.           fn value.value, val for fn in value.after
  19.         else
  20.           value.value          
  21.      
  22.       @before: (cb) ->
  23.         value.before.push cb
  24.         @
  25.       @after: (cb) ->
  26.         value.after.push cb
  27.         @
  28.       @subscribe: (cb) -> @after(cb)
  29.        
  30.      
  31.     if val? then getSet(val)
  32.     return getSet
  33. window.watch = fn.watch
  34.        
  35.  
  36.  
  37.  
  38.  
  39. a = fn.watch 42
  40. b = fn.watch 111111
  41. console.log 'a: ' + a()
  42. b(2222222)
  43. a(23)
  44. console.log 'b: ' + b()
  45. console.log 'a: ' + a()
  46. a.before (from, to) ->
  47.   alert 'change from: ' + from + ' to: ' + to
  48. a(55)
  49. console.log 'a: ' + a()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement