Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 设置';这';ajax回调中的值_Jquery_Ajax_Coffeescript - Fatal编程技术网

Jquery 设置';这';ajax回调中的值

Jquery 设置';这';ajax回调中的值,jquery,ajax,coffeescript,Jquery,Ajax,Coffeescript,我构建了一个jquery函数,它接受选项并发出ajax PUT请求。但是我在自定义成功回调时遇到了问题,因为它被重新定义了。有人知道如何保持“this”的价值吗 jquery函数 (($) -> $.textToInputUpdate = (options) -> functionality = options: $.extend({ 'id_for_put': "" 'post_url': ""

我构建了一个jquery函数,它接受选项并发出ajax PUT请求。但是我在自定义成功回调时遇到了问题,因为它被重新定义了。有人知道如何保持“this”的价值吗

jquery函数

 (($) ->
    $.textToInputUpdate = (options) ->
      functionality =
        options: $.extend({
          'id_for_put': ""
          'post_url': ""
          'size': 10
        }, options)
        initialize: (event, target) ->
          # ...
          self = this
          field.focusout (event) ->
            $.ajax
              url: self.options.post_url
              type: 'PUT'
              dataType: "json"
              data:
                rubric_item:
                  weight: parseFloat(field.val().trim())
              success: (data) ->
                self.options.success_callback?(data)

      return functionality
  ) jQuery
使用选项调用jquery函数

  $('#rubric').on 'click', '.rubric-item .rubric-value', (e) ->
    $.textToInputUpdate(
      id_for_put: $(this).parent().attr('id')
      post_url: $(this).parent().data("post-url")
      size: 3
      success_callback: (data) ->
        # PROBLEM HERE: $(this) gets redefined when actually called in the function above. I want it to be the value of $(.rubric-value).
        $(this).text(data.description)
    )
    .initialize(e, $(this))

您应该将此
分配给不同的变量,以便以后使用:

$('#rubric').on 'click', '.rubric-item .rubric-value', (e) ->
  var that = this;
  $.textToInputUpdate(
    id_for_put: $(this).parent().attr('id')
    post_url: $(this).parent().data("post-url")
    size: 3
    success_callback: (data) ->
      $(that).text(data.description)
  ).initialize(e, $(this))

您应该将此
分配给不同的变量,以便以后使用:

$('#rubric').on 'click', '.rubric-item .rubric-value', (e) ->
  var that = this;
  $.textToInputUpdate(
    id_for_put: $(this).parent().attr('id')
    post_url: $(this).parent().data("post-url")
    size: 3
    success_callback: (data) ->
      $(that).text(data.description)
  ).initialize(e, $(this))
只需使用:

self
变量更地道的咖啡脚本。

只需使用:


self
变量相比,coffeescript更为惯用。

self
似乎可以工作,不是吗?@Bergi肯定,但是如果您能够正确地进行绑定,那么在一系列回调膨胀的情况下,在相同的选项卡级别上将函数分成命名函数就更容易了。因此,即使你很懒,并且使用
self
self
似乎有效,知道这一点仍然很好,不是吗?@Bergi肯定,但是如果你能够正确地进行绑定,那么在一系列回调膨胀的情况下,在相同的选项卡级别上将函数划分为命名函数就更容易了。所以,即使你很懒并且使用
self
,知道这一点仍然很好。我不知道coffeescript中有fat arrow。这是一条路。我不知道《咖啡脚本》里有胖箭。这就是要走的路。为什么
这个
会改变?我有一个类似的问题,在我的完整回调中使用
this
,在回调中它从一个节点变为一个对象。为什么
this
会改变?我有一个类似的问题,我在完整回调中使用
this
,在回调中它从一个节点变为一个对象。