简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM