简体   繁体   English

javascript / D3基础:如果异步调用,为什么代码不在json请求之外?

[英]javascript/D3 basics: If asynchronous call, why is code outside of json request?

After reading variable scope in d3 javascript I assumed that in general you should place all your functions within the d3.json() {} request. 在d3 javascript中读取变量作用域后,我认为一般来说,您应该将所有函数放在d3.json() {}请求中。

I am working with some map zooming code from http://techslides.com/demos/d3/us-zoom-county.html and am trying to figure out why the clicked(d) function is outside of the d3.json request. 我正在使用来自http://techslides.com/demos/d3/us-zoom-county.html的一些地图缩放代码,并试图找出为什么clicked(d)函数不在d3.json请求中。

Additionally, why won't the code work with the clicked(d) function inside the d3.json request? 此外,为什么代码无法与d3.json请求中的clicked(d)函数d3.json

var width = 960,
    height = 500,
    centered;

var projection = d3.geo.albersUsa()
    .scale(1070)
    .translate([width / 2, height / 2]);

var path = d3.geo.path()
    .projection(projection);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("rect")
    .attr("class", "background")
    .attr("width", width)
    .attr("height", height)
    .on("click", clicked);

var g = svg.append("g");

d3.json("data/us.json", function(error, us) {


 g.append("g")
      .attr("id", "counties")
    .selectAll("path")
      .data(topojson.feature(us, us.objects.counties).features)
    .enter().append("path")
  .attr("d", path)
  .attr("class", "county-boundary")
      .on("click", countyclicked);

  g.append("g")
      .attr("id", "states")
    .selectAll("path")
      .data(topojson.feature(us, us.objects.states).features)
    .enter().append("path")
  .attr("d", path)
  .attr("class", "state")
      .on("click", clicked);


  g.append("path")
      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("id", "state-borders")
      .attr("d", path);
});


function clicked(d) {
  var x, y, k;

  if (d && centered !== d) {
    var centroid = path.centroid(d);
    x = centroid[0];
    y = centroid[1];
    k = 4;
    centered = d;
  } else {
    x = width / 2;
    y = height / 2;
    k = 1;
    centered = null;
  }

  g.selectAll("path")
      .classed("active", centered && function(d) { return d === centered; });

  g.transition()
      .duration(750)
      .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
      .style("stroke-width", 1.5 / k + "px");
}

function countyclicked(d) {
  alert(d.id);
}

I am trying to figure out why the clicked(d) function is outside of the d3.json request. 我试图弄清楚为什么clicked(d)函数不在d3.json请求之外。

Where a function is defined is mostly irrelevant, it just needs to be accessible from where it should be referenced. 定义函数的地方几乎无关紧要,只需要从引用位置就可以访问它。 clicked is accessible inside the d3.json callback, so that's fine. clicked是内部接近d3.json回调,所以这是很好。

The location of the function is only important if it is supposed to be a closure , ie needs access to values that are not passed as arguments. 函数的位置仅在假定为闭包时才重要,即需要访问未作为参数传递的值。 Eg if clicked needed to access us directly, it would have to be defined inside the callback. 例如,如果clicked需要直接访问us ,则必须在回调中定义它。
That's not the case here though, clicked gets all the data it needs via its parameter. 但是,这里不是这种情况, clicked通过其参数获取所需的所有数据。

However, what is important here is that the function is only being called after the data was received, since it is bound as event handler inside the callback ( .on("click", clicked) ) based on the data that was received. 然而,这里重要的是接收到数据 ,功能才会被调用,因为它必将为回调内部的事件处理程序( .on("click", clicked) )基于所接收到的数据。

why won't the code work with the clicked(d) function inside the d3.json request? 为什么代码不能与d3.json请求中的clicked(d)函数d3.json

I don't see any reason why that would be the case. 我看不出有什么理由。

That's because the function is also bound outside the callback to the container here: 这是因为该函数还在回调之外绑定到此处的容器:

svg.append("rect")
  .attr("class", "background")
  .attr("width", width)
  .attr("height", height)
  .on("click", clicked);

If you move inside the callback it is not accessible outside to that code anymore. 如果您在回调内部移动,则该代码外部将无法再访问它。

Note that clicked itself checks whether data is available or not by checking whether the argument d is set or not. 请注意, clicked本身通过检查参数d是否设置来检查数据是否可用。

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

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