简体   繁体   English

在对象文字中使用jquery.when

[英]Using jquery.when in an object literal

I've got a javascript/jQuery function called addLocation, which is a method of an object literal: 我有一个名为addLocation的javascript / jQuery函数,它是对象文字的一种方法:

addLocation: function(latLng, address=false, elevation=false, pan=false)
{

    $.when(geographic.getAddress(latLng), geographic.getElevation(latLng), geographic.getMagneticDeclination(latLng))
        .done(function(data){

            if (data.status != 'OK') {
                window.alert(data.message);
            }

            if (pan) {
                flyityourself.map.panTo(latLng);
                flyityourself.map.setZoom(16);
            }

            flyityourself.addWaypoint(latLng, data);

            flyityourself.addMarker(latLng, data);
        })
        .fail(function(msg) {
            window.alert(msg);
        }
    );
},

The three functions getAddress, getElevation and getMagneticDeclination are all methods of a second object literal named 'geographic'. 这三个函数getAddress,getElevation和getMagneticDeclination都是名为“ geographic”的第二个对象文字的方法。

But the code is failing. 但是代码失败了。

In the debugger, I have checked all four functions. 在调试器中,我检查了所有四个功能。

  • Each of getAddress, getElevation and getMagneticDeclination create $.Deferred objects and return $.Deferred.promise objects as they should. 每个getAddress,getElevation和getMagneticDeclination都会创建$ .Deferred对象,并按原样返回$ .Deferred.promise对象。
  • Each of getAddress, getElevation and getMagneticDeclination are retrieving the right data and are all reaching their respective resolve() statements. getAddress,getElevation和getMagneticDeclination中的每一个都在检索正确的数据,并且都到达各自的resolve()语句。
  • But in addLocation(), the data parameter only contains the data returned from getAddress. 但是在addLocation()中,data参数仅包含从getAddress返回的数据。

Previously, only the three methods getAddress, getElevation and getMagneticDeclination were in the object literal 'geographic'. 以前,只有三个方法getAddress,getElevation和getMagneticDeclination位于对象文字“地理”中。 addLocation was in a flat .js file. addLocation在一个纯.js文件中。 In this case the code worked. 在这种情况下,代码有效。

But since changing my code to put addLocation inside an object literal, the code has started to fail. 但是由于更改了我的代码以将addLocation放在对象文字中,所以代码开始失败。

Does $.when work differently in object literals or have I forgotten to qualify something? $ .when在对象字面量上的用法是否有所不同,还是我忘了限定某些内容?

Regards. 问候。 Chris B. 克里斯·B

A. Wolff is right in the comment they made - you need 3 params in your done callback, one for each promise handled by when. 答:Wolff在他们发表的评论中是正确的-您需要在完成的回调中添加3个参数,每个参数由when处理一次。

Here's a simple example, supplying a mock geographic object to make your code work in a stand-alone fiddle: 这是一个简单的示例,提供一个模拟地理对象以使您的代码在独立的小提琴中工作:

https://jsfiddle.net/tmLrn4gc/1/ https://jsfiddle.net/tmLrn4gc/1/

// Mock geographic object for testing
geographic = {
  // Return a promise, and a second later, resolve that promise with some
  // dummy data.
  getAddress: function() {
    var dfd = $.Deferred();
    window.setTimeout(function() {
      dfd.resolve("address:lat1,lng1")
    }, 1000);
    return dfd.promise();
  },
  getElevation: function() {
    var dfd = $.Deferred();
    window.setTimeout(function() {
      dfd.resolve("elevation:meters")
    }, 1000);
    return dfd.promise();
  },
  getMagneticDeclination: function() {
    var dfd = $.Deferred();
    window.setTimeout(function() {
      dfd.resolve("declination:degrees")
    }, 1000);
    return dfd.promise();
  }
};

// A simplified location manager, showing the 3 promises and a done method.
// The console output in the fiddle shows the 3 promises are resolved as
// desired.
LocationManager = {
  addLocation: function(latLng, address=false, elevation=false, pan=false) {
    $.when(
      geographic.getAddress(latLng),
      geographic.getElevation(latLng),
      geographic.getMagneticDeclination(latLng)
    ).done(function(d1, d2, d3) {
        console.log("Done", d1, d2, d3);
    })
  }
}

// Make it all happen!
LocationManager.addLocation();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM