是否可以重置jQuery延迟对象状态? [英] Is it possible to reset a jQuery deferred object state?
问题描述
是否可以将已解析的jQuery对象重置为未解析"状态,并重新开始其初始化和回调?
Is it possible to reset a resolved jQuery object to an 'unresolved' state and kick off it's initialization and callbacks all over again?
我正在做的特定事情是我在本地文件系统api上有一个jQuery延迟包装器.从那里我为我关心的事情建立了更高级别的递延:
The specific thing I'm doing is that I have a jQuery deferred wrapper over the local file system api. From there I build up higher level deferreds for the things I care about:
var getFs = defFs.requestQuota(PERSISTENT, 1024*1024)
.pipe (bytes) -> defFs.requestFs(PERSISTENT, bytes)
var getCacheContents = getFs.pipe (fileSystem) ->
defFs.getDirectory('Cache', fileSystem.root).pipe (dir) ->
defFs.readEntries(dir)
现在大多数时候,当我调用getCacheContents时,我不介意返回的备忘值,实际上,我更喜欢它.但是,当我想写入高速缓存时,我真的很希望能够重置该管道,并使其在下次访问时重新选择并重新扫描高速缓存.
Now most of the time, when I call getCacheContents I don't mind the memo-ized values being returned, in fact I prefer it. But, on the occasion when I want to write to the cache I really would like the ability to reset that pipe and have it re-select and rescan the cache next time its accessed.
我可以从$ .Callbacks中拼凑出一些东西,但是基于延迟的解决方案确实是理想的选择.
I could cobble something together from $.Callbacks but a deferred-based solution would really be ideal.
推荐答案
否. Promise
由定义定义,它只能解析一次-从unresolved
到rejected
.您将无法使用jQuery的Deferred
s.
No. A Promise
is by definition a thing that resolves only once - from unresolved
to fulfilled
OR to rejected
. You will not be able to do this with jQuery's Deferred
s.
您实际要搜索的是信号.他们将被解雇不止一次,但要提供类似的界面.周围有一些实现,您可以退出 js-signals
或
What you are actually searching for are Signals. They are to be fired more than once, but provide a similiar interface. There are some implementations around, you might ceck out js-signals
or wire.js
.
这篇关于是否可以重置jQuery延迟对象状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!