Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Last active December 23, 2015 14:09
Show Gist options
  • Select an option

  • Save westonplatter/6647243 to your computer and use it in GitHub Desktop.

Select an option

Save westonplatter/6647243 to your computer and use it in GitHub Desktop.
How to pass context into a AJAX request
#
# from a future commit on spree_wizard, https://github.com/think602/spree_wizard
#
SpreeWizard.Views.Steps ||= {}
class SpreeWizard.Views.Steps.IndexView extends Marionette.CompositeView
itemView: SpreeWizard.Views.Steps.StepView
template: JST["backbone/templates/steps/index"]
el: '#steps'
onRender: ->
_.each(@collection.models, ((model_step, index, list) ->
url_param_key = "step" + (index+1).toString()
variant_id = @getURLParameter(url_param_key)
@getVariant(model_step) if variant_id
), @)
getURLParameter: (name) ->
decodeURIComponent((new RegExp("[?|&]#{name}=([^&;]+?)(&|##|;|$)").exec(location.search) || [null,""] )[1].replace(/\+/g, '%20'))||null;
getVariant: (model_step) ->
view_step = @children.findByModel(model_step)
url = "/api/variants?q[id_eq]=" + model_step.get('id')
$.ajax(url,
dataType: 'json'
context: @
success: (data, textStatus, jqXHR) ->
@selectVariant(data.variants[0]) if data.count == 1
)
selectVariant: (variant_attributes) ->
console.log variant_attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment