Skip to content

Instantly share code, notes, and snippets.

@hayeah
Created November 20, 2010 21:50
Show Gist options
  • Select an option

  • Save hayeah/708206 to your computer and use it in GitHub Desktop.

Select an option

Save hayeah/708206 to your computer and use it in GitHub Desktop.
$ () ->
window.search = new LiveSearch("#search")
class LiveSearch
constructor: (context,result) ->
el = @context = $(context)
@result = el.find(".result")
@form = el.find("form")
@form.bind "ajax:success", (e,data) =>
window.data = data
@update($(data))
@checkboxes = @form.find(":checkbox")
@checkboxes.bind "change", () =>
@submit()
@sliders = @form.find(".slider").map (i,slider) => new Slider(this,slider)
submit: () ->
@form.callRemote()
update: (deals) ->
@result.find(".deals").replaceWith(deals)
class Slider
constructor: (@search,context) ->
el = @context = $(context)
# slider config
@slider = $("<div>")
@display = $("<div>")
el.append(@display)
el.append(@slider)
@options = $.parseJSON @context.find(".options").text()
@slider.slider(@options)
@slider.bind "slide", (event,ui) =>
@update(ui.values || ui.value || 0)
@slider.bind "slidechange", (event,ui) =>
@search.submit()
@input = el.find(":input")
@update(@options.values || 0)
update: (values) ->
@update_display(values)
@input.val values.toString()
update_display: (values) ->
# humanize the display according to specified type
if values instanceof Array
display = "#{@format(values[0])}, #{@format(values[1])}"
else
value = values
display = @format(value)
@display.html(display)
format: (value) ->
switch @options.format
when "million" then "$#{value / 1000000}M"
when "percent" then "#{value / 100}%"
else value.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment