Sha256: 8e4582cccb5e6ffa11366f91a7e2dfd380e0655c5b74a689a947c434070fd34e

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

gon._timers = {}

gon.watch = (name, possibleOptions, possibleCallback, possibleErrorCallback) ->
  return unless $?

  if typeof possibleOptions == 'object'
    options = {}
    for key, value of gon.watchedVariables[name]
      options[key] = value
    for key, value of possibleOptions
      options[key] = value
    callback = possibleCallback
    errorCallback = possibleErrorCallback
  else
    options = gon.watchedVariables[name]
    callback = possibleOptions
    errorCallback = possibleCallback

  performAjax = ->
    xhr = $.ajax
      type: options.type || 'GET'
      url: options.url
      data:
        _method: options.method
        gon_return_variable: true
        gon_watched_variable: name

    if errorCallback
      xhr.done(callback).fail(errorCallback);
    else
      xhr.done(callback)

  if options.interval
    timer = setInterval(performAjax, options.interval)
    gon._timers[name] ?= []
    return gon._timers[name].push
      timer: timer
      fn: callback
  else
    return performAjax()

gon.unwatch = (name, fn) ->
  for timer, index in gon._timers[name] when timer.fn == fn
    clearInterval(timer.timer)
    gon._timers[name].splice(index, 1)
    return

gon.unwatchAll = ->
  for variable, timers of gon._timers
    for timer in timers
      clearInterval(timer.timer)
  gon._timers = {}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gon-6.4.0 coffee/watch.coffee
gon-6.3.2 coffee/watch.coffee
gon-6.3.1 coffee/watch.coffee
gon-6.2.1 coffee/watch.coffee
gon-6.2.0 coffee/watch.coffee