sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

How to dynamically iterate through an OM's properties #83

Closed eladkas closed 6 years ago

eladkas commented 6 years ago

I am writing a generic OM collection config editor, that can edit collections of different kinds of OM's in my app. So I need to iterate through the properties (names, types and 'is mandatory') of an instance of an OM that I don't know its type, dynamically. How can I do that?

sylvainpolletvillard commented 6 years ago

Hi,

To retrieve the model from a model instance, see "How can I get the model from an instance ?" in http://objectmodel.js.org/#common-questions

Then, once you have a reference to the model, you can see its definition with yourObjectModel.definition

Example:

const User = ObjectModel({
    email: String, // mandatory
    name: [String] // optional
});

const stan = User({ email: "stan@smith.com" }); 

console.log(Object.keys(stan.constructor.definition)) // > ["email", "name"]
console.log(stan.constructor.definition.name) // [String]
sylvainpolletvillard commented 6 years ago

Can I close this issue ?

eladkas commented 6 years ago

Yes, thank you!

On Tue, Jul 17, 2018 at 8:56 PM Sylvain Pollet-Villard < notifications@github.com> wrote:

Can I close this issue ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sylvainpolletvillard/ObjectModel/issues/83#issuecomment-405707731, or mute the thread https://github.com/notifications/unsubscribe-auth/AlV20JgGeQkCFJyfpjVFeIQvh1cO_SMzks5uHkFmgaJpZM4VFRZ_ .

-- Elad Kaspin, Database Project Institute for Global Peace Work (IGP) Tamera Peace Research Center Monte do Cerro, 7630-392 Reliquias, Portugal Ph: +351-283 635 484 <javascript:void(0);> https://www.tamera.org

Follow us on Facebook https://www.facebook.com/terranovamovement/?fref=ts and Twitter https://twitter.com/TerraNovaRising