6

我一直在阅读这篇关于 EmberJS 中新增功能的文章。其中之一是Ember.Instrumentation,谁能解释我们在哪里使用它,如果可能的话,举个例子......谢谢

4

1 回答 1

9

为什么

一般来说,检测是一种通过订阅命名空间侦听器来测量应用程序性能和其他指标的方法。它也可用于调试。

例子

我不能把这个小提琴归功于我,我昨晚才在纽约 ember.js 聚会上看到它,但这应该提供一些背景信息:

http://jsfiddle.net/2Pn3f/6/

为了弄清楚是谁提出的,我只能找到他的聚会资料:http ://www.meetup.com/EmberJS-NYC/members/6706336/

要看到奇迹发生,请打开您的控制台并开始将学生标记为“这里”。

查看靠近顶部的 StudentView 和底部的 Em.Subscribe。

// In a view
Em.instrument("student.here", this.get('content'), function() {
    //mark student as in attendance
    this.set('inAttendance', !this.get('inAttendance'));
  }, this);
},

...

Em.subscribe('*', {
  ts: null,
  before: function(name, timestamp, payload) {
    ts = timestamp;
    //console.log('    before: ', name, JSON.stringify(payload));
    //return 'HelloFromThePast';
  },
  after: function(name, timestamp, payload, beforeRet) {
    //log metrics
    //record analytics
    //profile app
    console.log('instrument: ', name, JSON.stringify(payload), beforeRet, timestamp - ts);
  }
});

边注

更酷的是,您可以使用通配符订阅 ember 对 Instrumentation 的使用。

http://jsfiddle.net/dmazza/sUvdg/

文档

有关详细信息,请参阅文档: http: //emberjs.com/api/classes/Ember.Instrumentation.html

于 2012-10-26T20:39:32.273 回答