Skip to content

Instantly share code, notes, and snippets.

@adambard
Created August 8, 2012 05:02
Show Gist options
  • Save adambard/3292225 to your computer and use it in GitHub Desktop.
Save adambard/3292225 to your computer and use it in GitHub Desktop.
Coffeescript backing franslate.me
jQuery ->
class QuestionView extends Backbone.View
el: $('#main')
tmpl: mustache_templates.question
url: ''
events:
"click .answer": "check_answer"
initialize: (language, difficulty) ->
@url = "/#{language}/#{difficulty}/"
current_html = $(@el).html()
window.history.pushState({name: "Homepage"}, "Homepage", "/")
window.onpopstate = =>
window.location = "/"
_.bindAll @
@fetch_q()
fetch_q: ->
uniqueness_token = (new Date).getTime().toString()
$.ajax(
method: 'get'
url: @url + "?" + uniqueness_token
success: (data) =>
@q = data
@render()
)
check_answer: (e) ->
$('.incorrect', @el).remove()
$('.correct', @el).remove()
if $(e.target).html() == @q['correct_option']
$('h2', @el).after($('<div class="correct">Correct!</div>'))
setTimeout(@fetch_q, 500)
else
$('h2', @el).after($('<div class="incorrect">Incorrect. Please try again.</div>'))
render: ->
console.log(@q)
$(@el).html(Mustache.render(@tmpl, @q))
$('.initiate-quiz').click ->
parts = @id.split('-')
window.question = new QuestionView(parts[0], parts[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment