Skip to content

Instantly share code, notes, and snippets.

@jeffrydegrande
Created January 5, 2013 21:04
Show Gist options
  • Save jeffrydegrande/4463670 to your computer and use it in GitHub Desktop.
Save jeffrydegrande/4463670 to your computer and use it in GitHub Desktop.
# Wrapper around jQuery ajax methods that exposes
# returned data by publishing an event
#
class Ajax
@getJSON: (url, event)->
$.getJSON url, (data)->
Event.trigger(event, data)
window.Ajax = Ajax
# Public: tiny wrapper on javascript events
#
class Event
# Public: register an event handler
#
# event - The name of the event
# data - optional data passed along with the event
#
# Example:
#
# Event.on 'stuff:received', (data)-> doSomethingWith(data)
#
@on: (event, fn)->
$('body').bind event, (_, data) -> fn(data)
# Public: trigger an event
#
# event - The name of the event
# data - data to pass along
#
# Example:
#
# Event.trigger 'stuff:received', data
#
@trigger: (event, data)->
$('body').trigger(event, [data])
window.Event = Event
#= require dashboard/ajax
#= require dashboard/event
#= require dashboard/map_utils
#
#= require dashboard/tracker
#= require dashboard/reference_point
$ ->
map = new Map()
Event.on 'trackers:ready', (trackers)->
Tracker.reset(map, trackers)
Event.on 'trackers:filtered', (trackers)->
Tracker.update(map, trackers)
Tracker.fetch()
#= require dashboard/ajax
#= require dashboard/tracker_list
class Tracker
@fetch: -> Ajax.getJSON "/current_positions", "trackers:ready"
@reset: (map, trackers)->
@trackers = trackers
@updateUI(map, trackers)
@update: (map, trackers)->
@updateUI(map, trackers)
@updateUI: (map, trackers)->
TrackerList.update(trackers)
for tracker in trackers
map.show(@markerForTracker(tracker))
toHide = _.difference(@trackers, trackers)
$.each toHide, (_, tracker)->
map.hide(@markerForTracker(tracker))
@markerForTracker: (tracker)->
if not tracker.marker
label = tracker.name
location = LatLng.of(tracker.most_recent_message)
tracker.marker = createMarker location, label,
draggable: false,
raiseOnDrag: false
tracker.marker
@filter: (fn)->
Event.trigger 'trackers:filtered', _.filter(@trackers, fn)
window.Tracker = Tracker
# Private: filter by name or uin
# Returns a function for use with _.filter
filterByString = (text)->
search = new RegExp(text, 'i')
(tracker)-> search.test(tracker.name) || search.test(tracker.uin)
# Private: filter by company
# Returns a function for use with _.filter
filterByCompany = (company)->
(tracker)-> tracker.company == company
$ ->
$('#search_filter').bind 'keyup', ->
nameOrUIN = $(this).val()
Tracker.filter(filterByString(nameOrUIN))
class TrackerList
@update: (trackers)->
conversion = (tracker)->
id: tracker.uin,
value: "#{tracker.name} - #{tracker.uin}"
$('#trackers_list').setOptionsFromCollection(trackers, true, conversion)
window.TrackerList = TrackerList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment