How to retrieve all properties of an Ember.js model

14,459

Solution 1

You can simply use toJSON method on model and get the keys from object.

Ember.keys(model.toJSON())

Note that will not return you keys for relations.

Solution 2

You can also use this:
http://emberjs.com/api/data/classes/DS.Model.html#property_attributes http://emberjs.com/api/data/classes/DS.Model.html#method_eachAttribute

Ember.get(App.User, 'attributes').map(function(name) { return name; });
Ember.get(userInstance.constructor, 'attributes').map(function(name) { return name; });

There's also similar properties for relationships too.

Solution 3

An easy way to print out the fields and their values:

Ember.keys(model.toJSON()).forEach(function(prop) { console.log(prop + " " + model.get(prop)); } )
Share:
14,459
joscas
Author by

joscas

Updated on June 16, 2022

Comments

  • joscas
    joscas almost 2 years

    I'm working with forms in Ember.js and I want to retrieve a list of all model properties so that I can take snapshots of the state of the form at different moments. Is there a way to get a list of all properties of a model?

    For example, if my model is:

    App.User = DS.Model.extend({
      name: DS.attr('string'),
      email: DS.attr('string'),
      current_password: DS.attr('string'),
      password: DS.attr('string'),
      password_confirmation: DS.attr('string'),
      admin: DS.attr('boolean'),
    }
    

    Then I would like to have something like this:

    > getEmberProps('User')
    
    ["name", "email", "current_password", "password", "password_confirmation", "admin"]
    
    • marko
      marko about 11 years
      JSON.stringify? Have you tried that?
    • joscas
      joscas about 11 years
      hmmm, I wouldn't know how to JSON.stringify that to an Ember model
  • joscas
    joscas about 11 years
    Thanks @mavilein What I don't like of this solution is having to use a mixin. I've come up myself with something that seems to work (see my edit). Could your solution work without having to extend from a mixin?
  • mavilein
    mavilein about 11 years
    If you don't like in the mixin, it should be easy to extract the code into a separate function with the model as the argument. But i don't understand, why you don't like the mixin. This is probably the most emberish way to solve this problem.
  • joscas
    joscas about 11 years
    Well, maybe I'm getting confused but if I use the mixin in this way, what I get is the properties of the object instance but I can't do that: User = DS.Model.extend(Ember.AllKeysMixin,{ name: DS.attr('string'), email: DS.attr('string'), current_password: DS.attr('string'), password: DS.attr('string'), password_confirmation: DS.attr('string'), admin: DS.attr('boolean'), and then model.getKeys()somewhere else to get 'name', 'email', etc..
  • mavilein
    mavilein about 11 years
    Ah, i did not understand your question that way. So you want to get the properties of the class and not those of the instance. When i read model, i am always thinking about the instance. So my approach won't work. Maybe one could migrate this function relatively easy to the class by adding it so a class via reopenClass()..
  • joscas
    joscas about 11 years
    Maybe the title of my question is misleading. If someone can think of a better title I will be happy to edit it.
  • Jacob van Lingen
    Jacob van Lingen about 9 years
    Note that will not return you keys for relations -> I don't know which ember version you are using; I am using Ember 1.11.3 at the moment, for that version relations keys are returned as well.