Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js -- how to console log a model

Pretty general question here. In my code I am frequently dealing with models:

let model = this.currentModel;

Which seems to be working, but if I

console.log(model);

I see this useless code in the console:

<lc-dash@model:bizinfo::ember904:null>

Does anyone know how to actually log the contents of the model as an object? Also, anywhere I can read about the meaning of this tag?

like image 708
Daniel Thompson Avatar asked Sep 06 '25 03:09

Daniel Thompson


1 Answers

Does anyone know how to actually log the contents of the model as an object?

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

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 defined in the store's adapter to create a JSON representation of the model.

Also, anywhere I can read about the meaning of this tag?

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

<lc-dash@model:bizinfo::ember904:null>

means:

  • lc-dash is your app name
  • model is the ember type of the object you are logging (can be controller, route etc.)
  • bizinfo is the name of the object you are logging (name of your model, or controller, or route etc.)
  • ember904 is a guId create with Ember.guidFor
  • null is the model's id. You can overwrite this value using the method toStringExtension in your particular model

For comparison example, here's how logging your application controller would look:

<lc-dash@controller:application::ember324>
like image 88
nem035 Avatar answered Sep 07 '25 21:09

nem035