有没有一种方法可以触发另一个模块中可能“调用"的方法的调用.存在? [英] Is there a way to trigger call a method in another module that "might" exist?

查看:131
本文介绍了有没有一种方法可以触发另一个模块中可能“调用"的方法的调用.存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个基本问题...但是我会尽力解释.

I know this is a base question... but I'll try to explain.

我一直在使用Deferred,但有人指出我正在将其用作反模式. 基本上,我可以在子模块中使用deferred.但是,如果这是一个反模式,那么实现此目标的最佳方法是什么,其中子模块"可能不存在..因此,我不想在成功使用父ajax时显式调用子方法打电话.

I've been using a Deferred, but someone pointed out that I'm using it as an anti-pattern. Basically, I can use the deferred in the child module. BUT if this is an anti-pattern, what is the best way to achieve this in which the "child module" might not be there.. SO, I don't want to explicitly call the child method in the success of the parent ajax call.

所以,三个问题:

  1. 这是否是一种可行的方法-实现我要追求的目标的好方法是什么?

  1. Is this a viable way, if not - what is a good way to achieve what I am going for?

即使我重置了Deferred,它似乎也没有重置".为什么?

Even though I am resetting the Deferred, it isn't seeming to 'reset'. Why?

CAN也可以通过将其与另一个事件(即onload)耦合来实现. SO <我们让ajax和onload做同样的事情.

CAN this also be achieved coupling it with another event - ie, onload. SO< we have the ajax AND onload doing the same thing.

我有一个父模块.

var Z = {};

Z.someVar = $.Deferred();

Z.a = function(){
    //methods

    //Ajax call on page: can be once or multiple calls.
    $AJAX = {
        url:...,
        dataType: ...,
        //etc..
    }.success(
      function(){
         Z.someVar.resolve();
      }
    ).error();


};

// another method that MIGHT exist.

Z.b = function(){
   var func1 = function(){
       // do some stuff
       Z.someVar = $.Deferred(); // reset it.
   }
   Z.someVar.done(function(){
      func1();
   })
}

推荐答案

promise是对单个,一次值计算的抽象.许诺开始待定,然后可以转换为已实现已拒绝状态.承诺一旦解决(过渡),便永远无法再更改其状态.

A promise is an abstraction over a single, one time calculation of a value. A promise starts pending, and then can transition to either fulfilled or rejected state. Once a promise resolved (transitioned) it can never change its state again.

递延仅用一种-当您要从不是promise *的东西构造promise时.

Deferreds are used in exactly one - when you want to construct a promise from something that's not a promise*.

在jQuery $.ajax中已经返回一个开始的承诺.

In jQuery $.ajax already returns a promise to begin with.

Z.a = function(){ // this method now returns a promise
    return $.ajax({...});
};

如果要链接它,可以使用.then:

If we want to chain it, we can use .then :

Z.a().then(function(){
    // here a() is done.
});

您还可以使用$.when(大多数promise库中的Promise.resolve)使用缓存模式:

You can also use a caching pattern, using $.when (Promise.resolve in most promise libraries):

Z._a = null;
Z.a = function(){
    if(z._a){ // already have the value cached:
        return $.when(z._a); // promise over the value
    }
    return $.ajax({...}).then(function(res){
        Z._a = res;
        return res; // chain
    });

};

Z.b = function(){
    return Z.a().then(function(aValue){
        // do something with value, your return value here is the resolution
        // value of the a method call.
    });
}

现在,如果您要等待多个呼叫,则可以将$.when与多个promise一起使用并获取其结果.

Now, if you want to wait for multiple calls, you can use $.when with multiple promises and get their results.

还有其他一些非常罕见的带有聚合的用例

这篇关于有没有一种方法可以触发另一个模块中可能“调用"的方法的调用.存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆