[关闭]
@lizlalala 2016-08-08T06:00:56.000000Z 字数 3522 阅读 873

实习杂项

jquery ajax jsonp


  1. jquery load/unload事件
    $(window).load()
  1. Will execute after the page along with all its contents are done loading. This means that all images, CSS (and content defined by CSS like custom fonts and images), scripts, etc. are all loaded. This happens event fires when your browser's "Stop" -icon becomes gray, so to speak. This is very useful to detect when the document along with all its contents are loaded.

$(document).ready()

  1. This on the other hand will fire as soon as the web browser is capable of running your JavaScript, which happens after the parser is done with the DOM. This is useful if you want to execute JavaScript as soon as possible.

$(window).unload()

  1. This event will be fired when you are navigating off the page. That could be Refresh/F5, pressing the previous page button, navigating to another website or closing the entire tab/window.

To sum up

  1. ready() will be fired before load(), and unload() will be the last to be fired.

2,encodeUricomponent的正确用法:
只针对key,value中的value进行局部编码

It's worth pointing out that encodeURIComponent("var1=value1&var2=value2") is not the typical use case. That example will encode the = and &, which is probably not what was intended! encodeURIComponent is typically applied separately to just the value in each key value pair (the part after each =).

3,注意事项
离开事件(beforeunload/unload)是不能自定义提示diaLog的,只能返回值来替换里面的值。

  1. $(window).on("beforeunload", function(event) {
  2. var isAllSaved = cityOverlays.every(function(overlay) {
  3. return overlay.saved === true;
  4. });
  5. // console.log(isAllSaved);
  6. // setLocalStorage("北京");
  7. console.log(" all saved ", isAllSaved);
  8. if (!isAllSaved) {
  9. (event || window.event).returnValue = "您有未保存的围栏,确定离开吗?"; //Gecko + IE
  10. return "您有未保存的围栏,确定离开吗?";
  11. // showPopTips(null,"离开");
  12. }
  13. });
  14. $(window).on("unload", function(event) {
  15. console.log("will set localStorage", cityOverlays);
  16. setLocalStorage("北京");
  17. });
  18. window.addEventListener("beforeunload", function(e) {
  19. var isAllSaved = cityOverlays.every(function(overlay) {
  20. return overlay.saved === true;
  21. });
  22. if (!isAllSaved) {
  23. // setLocalStorage(cityInput.val())
  24. console.log("有围栏未保存");
  25. (e || window.event).returnValue = "您有未保存的围栏,确定离开吗?"; //Gecko + IE
  26. return "您有未保存的围栏,确定离开吗?"; //Webkit, Safari, Chrome
  27. // showPopTips(null,"离开");
  28. } else {
  29. console.log("已保存");
  30. return null;
  31. // setLocalStorage(cityInput.val());
  32. } //Webkit, Safari, Chrome etc.
  33. });

4.ajax跨域的问题。

  1. $.ajax({
  2. url: url + queries,
  3. type: "GET",
  4. dataType: "jsonp",
  5. jsonp:"callback",//jsonp请求的callback的函数名(key值)
  6. jsonpCallback:"handleServerHeatData",//调用的函数名(value值)
  7. success: function(serverData) {
  8. debugger;
  9. if (serverData&& serverData.errno == 0) {
  10. showHotMap(serverData.data);
  11. drawPolygons(cityInput.val());
  12. } else {
  13. alert("获取热力图数据失败");
  14. }
  15. myTool.loadingPopupClose();
  16. },
  17. error: function(xhr, errorType, error) {
  18. debugger;
  19. console.log(error);
  20. }
  21. });

jquery中对jsonp类型的ajax类型的请求处理:本来应该取得数据后调用handleServerHeatData,现在自动帮你生成回调函数并把数据取出来供success属性方法来调用。
然后发ajax的时候的url就是这样的

  1. url + queries
  2. http://100.69.123.33:8008/hotmap/getHeatmapOverlay?key=value1&key2=value2?callback=handleServerHeatData

tutorial

  1. 删除object里面的key,value
    delete obj[key]
  1. var a = {"name1":"reer","name2":"hh","name3":"hh"};
  2. delete a['name2'];
  3. //result
  4. a
  5. Object {name1: "reer", name3: "hh"}
  1. 取小数点后两位
    solution1:
  1. Math.round(num*100)/100)

solution2:

  1. num.toFixed(2)
  2. eg.
  3. var a = 22.446;
  4. a.toFixed(2) => "22.45"

7.
module.exports = {
getUserPolygon: getUserPolygon,
userId:userId,
userName:userName
}
逻辑上先调用getUserPolygon。然后获取到userId这些
如果把它们export的话,外部不一定能获取到userId的值
更新
其实不是这样的,要使得外部能获取,有两种方式:
前提是模块+同步
(1)module内部直接就执行函数了。
(2)暴露出来的不是变量,而应该是一个get函数

如果没有模块的话,很好处理是吧,就是promise就可以。
但是如果模块的话。。就得设成同步。

  1. echarts的setoption(如果只是最开始的时候init的无data的那些option).需要把data设为[]的,而不能直接不设置

9.jsonp跨域的问题
[] jsonp发送的其实是get请求,不能跟post连起来用。这部分不太懂todo

10.

数据库一致性问题,更新/删除结束后再刷新并没有更新

  1. 可拖动

  2. 借助jquery-ui。使用方法:
    step1:
    引入js文件
  1. <script src="/static/hotmap/js/libs/jquery/jquery-ui.min.js"></script>

step2

  1. $(".pop-chart-box").draggable();

哪个想要拖动就设置在哪一个上面

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注