Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.Instrumentation API example

I have been going through this post regarding newly added features in EmberJS. One of them being Ember.Instrumentation, can anyone explain where do we use it, if possible with an example...Thanks

like image 232
Mudassir Ali Avatar asked Oct 16 '12 17:10

Mudassir Ali


1 Answers

Why

Instrumentation in general is a way to measure performance and other metrics in your app by subscribing to namespaced listeners. It can also be useful for debugging.

Example

I can't take credit for making this fiddle, I only saw it last night at the NYC ember.js meetup, but this should provide some context:

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

In my attempt to figure out who presented this, I could only find his meetup profile: http://www.meetup.com/EmberJS-NYC/members/6706336/

To see the magic happen, open your console and start marking students as 'here.'

See the StudentView near the top and Em.Subscribe at the bottom.

// 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);
  }
});

Side note

What's even cooler is that you can subscribe to ember's use of instrumentation by using the wildcard.

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

Documentation

See the documentation for details: http://emberjs.com/api/classes/Ember.Instrumentation.html

like image 197
dmzza Avatar answered Sep 28 '22 10:09

dmzza