Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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';s Deferred.promise()替换对象';现在的承诺是什么?_Jquery_Jquery Deferred - Fatal编程技术网

我可以使用jQuery';s Deferred.promise()替换对象';现在的承诺是什么?

我可以使用jQuery';s Deferred.promise()替换对象';现在的承诺是什么?,jquery,jquery-deferred,Jquery,Jquery Deferred,根据jQuery文档中关于推迟的承诺([目标]): 如果提供了target,deferred.promise()将把方法附加到 然后返回该对象,而不是创建新对象。这个可以 将承诺行为附加到已经存在的对象时非常有用 存在 据我所知,在目标上调用promise()应该会替换目标现有的promise接口,在这种情况下,我希望下面记录“new done”: 但我仍在获得目标的原始完成回调。(使用jQuery 1.8.3。) 我的理解是否完全不正确,或者是否有某种方法可以以某种方式替换对象的整个promi

根据jQuery文档中关于推迟的承诺([目标]):

如果提供了target,deferred.promise()将把方法附加到 然后返回该对象,而不是创建新对象。这个可以 将承诺行为附加到已经存在的对象时非常有用 存在

据我所知,在目标上调用
promise()
应该会替换目标现有的promise接口,在这种情况下,我希望下面记录
“new done”

但我仍在获得目标的原始
完成
回调。(使用jQuery 1.8.3。)


我的理解是否完全不正确,或者是否有某种方法可以以某种方式替换对象的整个promise接口?

您正在替换该getJSON调用的基于promise的接口,但该替换是在您启动请求并已附加“old done”回调之后发生的。另外,原始的getJSON延迟仍然是在请求完成时解析的,这就是为什么它会触发而您的不会

为了完成您正在尝试的工作,您必须在附加任何回调之前替换接口。您还必须在请求完成时手动解决您提供的延迟请求

通常情况下,类似的方法会起作用:

var deferred = $.Deferred();
var request = $.getJSON( "url" );

request.then(
  function() {
    deferred.resolveWith( this, arguments );
  },

  function() {
    deferred.rejectWith( this, arguments );
  }
);

// Now it's safe to replace the promise methods on the request
deferred.promise( request );

// This callback is being attached to the Deferred we provided,
// not the one managed internally by getJSON
request.done(function() {
    console.log( "Done!" );
});

此方法仅对.done、.fail和.always安全。其他不推荐使用的延迟处理程序(success、error、complete)不在本例中处理,不过如果您愿意的话,修复它们并不困难。

您正在替换该getJSON调用的基于承诺的接口,但该替换是在您启动请求并已附加“old done”回调之后发生的。另外,原始的getJSON延迟仍然是在请求完成时解析的,这就是为什么它会触发而您的不会

为了完成您正在尝试的工作,您必须在附加任何回调之前替换接口。您还必须在请求完成时手动解决您提供的延迟请求

通常情况下,类似的方法会起作用:

var deferred = $.Deferred();
var request = $.getJSON( "url" );

request.then(
  function() {
    deferred.resolveWith( this, arguments );
  },

  function() {
    deferred.rejectWith( this, arguments );
  }
);

// Now it's safe to replace the promise methods on the request
deferred.promise( request );

// This callback is being attached to the Deferred we provided,
// not the one managed internally by getJSON
request.done(function() {
    console.log( "Done!" );
});

此方法仅对.done、.fail和.always安全。其他不推荐使用的延迟处理程序(success、error、complete)不在本例中处理,但如果您愿意的话,修复它们并不困难。

谢谢,这肯定有助于澄清问题。我有一个额外的转折点…谢谢,这肯定有助于澄清问题。我有一个额外的扭曲。。。