ember.js

Debugging

Logging EmberData

The ember data models have a toJSON method that extracts the relevant data:

console.log(model.toJSON());

This method uses the JSONSerializer to create the JSON representation.

If you want to log the data in a more app-specific way, you can use serialize:

model.serialize();

which uses the serialization strategy you can define in the store’s adapter to create a JSON representation of the model.

All objects in an Ember app, including Ember Data models, inherit from Ember.CoreObject, which has a toString method that prints this representation:

<app-name@ember-type:object-name:id>

Explanation:

  • app-name is the name of your application
  • ember-type is the ember type of the object you are logging (can be controller, route etc.)
  • object-name is the name of the object you are logging (name of your model, or controller, or route etc.)
  • id is either a guId create with Ember.guidFor or, for example, the model’s id.

You can overwrite this value using the method toStringExtension in your particular model.

For comparison example, here’s how logging an application controller could look:

<my-awesome-app@controller:application::ember324>

Running debug-only code

Ember has a static global method called runInDebug which can run a function meant for debugging.

Ember.runInDebug(() => {
  // this code only runs in dev mode
});

In a production build, this method is defined as an empty function (NOP). Uses of this method in Ember itself are stripped from the ember.prod.js build.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow