javascript - Ajax setting 'context' does not affect 'data's location -
the following ajax works
function retrieve(el) { var table = this; this.el = el; this.fetchinfo = function () { $.ajax('data.html', { context: table, <!-- question here --> data: { location: table.data('location') }, success: function (response) { this.el.find('.info').html(response).fadein(); }, } }
but wondering why cannot replace table.data
this.data
on denoted line. since set context table
variable, this
should set whatever table
referring right? holds true in context of other members of ajax object (including success
), not members of data
. why case?
data('name')
extracts value tag attribute data-name
the context
variable you've given applies within success
callback, , doesn't change of other parameters passed $.ajax
.
the answer therefore depends on how call fetchinfo
. data:
variables resolved in whatever context fetchinfo
has. given you're experiencing problems, suggests not calling function retrieve
object context.
edit line problem:
this.el.on('click', 'button', this.fetchinfo);
just because you've referred this.fetchinfo
, doesn't make this
context when it's subsequently invoked. try instead:
this.el.on('click', 'button', $.proxy(this.fetchinfo, this));
Comments
Post a Comment