Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

暴露 window.detector 全局对象 #43

Closed
hotoo opened this issue Mar 2, 2014 · 21 comments
Closed

暴露 window.detector 全局对象 #43

hotoo opened this issue Mar 2, 2014 · 21 comments
Assignees
Milestone

Comments

@hotoo
Copy link
Owner

hotoo commented Mar 2, 2014

  1. Tracker 要改造,切换到使用 detector。
  2. Tracker 要同时适应 CMD 和非 CMD 环境。
// tracker.js
(function(global){

  var detector;
  var Tracker = function(){};

  if("function" === typeof define){
    define(function(require, exports, module){
      detector = require("detector");
      module.exports = Tracker;
    })
  }else{
    detector = global.detector;
  }

  // 注意:这里要发送数据,部分由 detector 提供。
  // 但是此时 define 还没执行,detector 为 undefined。
  send(detector);

})(this);
@hotoo hotoo added this to the 1.4.0 milestone Mar 2, 2014
@hotoo
Copy link
Owner Author

hotoo commented Mar 2, 2014

想了下,可以改造如下,就不用 detector 暴露全局对象了:

// tracker.js
(function(global){

  var detector;
  var Tracker = function(){};

  if("function" === typeof define){
    define(function(require, exports, module){
      detector = require("detector");
      sendData(detector);
      module.exports = Tracker;
    })
  }else{
    detector = global.detector;
    sendData(detector);
  }

 function sendData(detector){
    send(detector);
  }

})(this);

不过如果暴露全局对象,像浏览器升级提示这种需求,就不用这么麻烦了。
我觉得对于 detector 来说,不区分是否处于 CMD 环境中,统一暴露全局对象带来好处,如简单、一致性,值得占用一个全局变量。

@hotoo hotoo self-assigned this Mar 2, 2014
@lifesinger
Copy link

直接暴露的问题还是「冲突隐患」,因为 detector、tracker、monitor 这些脚本,长久来说,如果发展得好,很可能部署到整个集团,甚至外部网站也能直接用。这些情况下,暴露的全局变量名,就很关键。

比如 google 分析的部署脚本,只占用 _gaq 变量,冲突概率极小,部署也很灵活:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-36247332-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    if (location.port) {
       return;
    }
    ga.src = 'https://ssl.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

从保持简单性上考虑,我的建议:

  1. detector 不需要考虑 XMD 环境,直接暴露全局变量,但这个全局变量本身要尽量避免冲突,比如 _detector,或取比较有创意的名字 probe,或用缩写 ced(Client Environment Detector)之类的,避免用大家都会比较容易想到的问题,比如 tracker、monitor 等(阿里就一堆)
  2. detector 本身在 CMD 环境下的获取,通过 CMD loader 自身去实现。很久以前,有人建议:
require('detector')  应该内部实现成  require('detector') || window['detector']

sea.js 可以考虑修改成这种方式(还需要进一步讨论确定)

  1. detector、monitor、tracker 等去依赖,原生化。应该向 ga.js 的方向努力。

@hotoo
Copy link
Owner Author

hotoo commented Mar 3, 2014

OK, 收到。

求取名。

  • arale_detector ?

@lifesinger
Copy link

取名是最头疼的事……

@lizzie
Copy link
Contributor

lizzie commented Mar 3, 2014

require('detector') 应该内部实现成 require('detector') || window['detector']

是不是反了? 按我理解应该是 require('detector') 时, 应先判断 window['detector'] 是否存在, 否的话再 require('detector')

@lifesinger
Copy link

没反,乔花。局部优先,没有时才到全局看一下。和作用域一样的。

On Mon, Mar 3, 2014 at 2:27 PM, Yan notifications@github.com wrote:

require('detector') 应该内部实现成 require('detector') || window['detector']

是不是反了? 按我理解应该是 require('detector') 时, 应先判断 window['detector'] 是否存在, 否的话再
require('detector')


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-36484594
.

王保平 / 玉伯(射雕)
送人玫瑰手有余香

@hotoo
Copy link
Owner Author

hotoo commented Mar 3, 2014

别跑题,快想名字~

@lizzie
Copy link
Contributor

lizzie commented Mar 3, 2014

_spy
_trace
或者索性 _hotoo 好啦. .啊哈哈~~

@lifesinger
Copy link

g_detector ?

g 是 global 的意思。甚至后续可以约定,我们的模块,如果需要往全局暴露变量,都遵守 g_lowercase_lowercase 这种格式。

On Mon, Mar 3, 2014 at 10:01 PM, Yan notifications@github.com wrote:

_spy
_trace
或者索性 _hotoo 好啦. .啊哈哈


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-36512038
.

王保平 / 玉伯(射雕)
送人玫瑰手有余香

@lepture
Copy link
Contributor

lepture commented Mar 4, 2014

@lifesinger 不好看

@lifesinger
Copy link

南伯给个好看的建议

On Tue, Mar 4, 2014 at 9:10 AM, Hsiaoming Yang notifications@github.comwrote:

@lifesinger https://github.com/lifesinger 不好看


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-36581477
.

王保平 / 玉伯(射雕)
送人玫瑰手有余香

@lepture
Copy link
Contributor

lepture commented Mar 4, 2014

tan.js (探.js) 如何?短,好寫,好記。

@hotoo
Copy link
Owner Author

hotoo commented Mar 4, 2014

不错。

不过这里讨论的不是模块名称,而是暴露的全局变量名,window.tan 或 window.Tan 冲突的概率大不大?

@lepture
Copy link
Contributor

lepture commented Mar 4, 2014

@hotoo 我覺得不大。應該沒人用這個名字。如果覺得有的話,可以用 _tan 呀。

@lizzie
Copy link
Contributor

lizzie commented Mar 4, 2014

那考虑 _spy 啊. 冲突的话用 _i_spy 或者其他的前缀

@hotoo
Copy link
Owner Author

hotoo commented Mar 4, 2014

@lizzie _spy 太容易冲突,也不合适。detector 是探测识别发现,跟间谍刺探完全不同。
我觉得 @lepture 的提议非常好,如无更合适的,就叫 Tan 了。

@hotoo
Copy link
Owner Author

hotoo commented Mar 4, 2014

不过话说放弃 detector 的名字有点不舍~

@hotoo
Copy link
Owner Author

hotoo commented Mar 8, 2014

经组织讨论决定,为了规范统一,组件名称保持 detector 不变,暴露的全局变量使用 g_ 开头。
例如 detector 组件暴露 window.detectorwindow.g_detector 全局变量。

@afc163
Copy link
Contributor

afc163 commented Mar 8, 2014

暴露的全局变量使用 g_ 开头。
例如 detector 组件暴露 window.detector

这两句自相冒顿啊。
为啥要暴露两个,detector 这个太容易冲突了。

@hotoo
Copy link
Owner Author

hotoo commented Mar 8, 2014

之前在非 cmd 环境已经暴露了 detector,所以保留了这个。
将来 Tracker 也会保留已有的全局变量。
未来新的代码推荐使用 g_ 开头的。

@hotoo
Copy link
Owner Author

hotoo commented Dec 18, 2014

detector 代码本身不再暴露全局变量,如有需要,可以使用 zpm 或者自行定制编译。

@hotoo hotoo reopened this Dec 18, 2014
@hotoo hotoo modified the milestones: 1.4.0, 2.2.0 Dec 18, 2014
@hotoo hotoo removed this from the 1.4.0 milestone Dec 18, 2014
@hotoo hotoo closed this as completed in 578d4a3 Dec 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants