backbone.js - Accessing view from within ajax success() call -
backbone.js - Accessing view from within ajax success() call -
i have backbone.js view has next ajax call:
backbonedemo.views.projects.projectview extends backbone.view demofunction: () -> ... ... @collection.create(new_project_attributes, success: -> console.log @ $('#' + @options.query_id).html('saved successfully').delay(1500).fadeout(500, -> $(@).remove())
the problem is, this
within success phone call returns domwindow
, not view in @options.query_id
located. (yes, demofunction
bound view).
how can access view attributes within ajax success()
phone call in backbone?
the idiomatic coffeescript way utilize fat arrow bind callback function this
want be:
the fat arrow =>
can used both define function, , bind current value of this
, right on spot. helpful when using callback-based libraries prototype or jquery, ...
so this:
@collection.create(new_project_attributes, success: => console.log @ $('#' + @options.query_id).html('saved successfully').delay(1500).fadeout(500, -> $(@).remove())
looks want this
element you're fading out in inner callback you'd leave lean arrow (->
) alone.
and simple demo of difference between =>
, ->
: http://jsfiddle.net/ambiguous/vghrm/
backbone.js coffeescript
Comments
Post a Comment