sylvainpolletvillard / ObjectModel

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

Proposal: An easy way to add new properties to objects after instantiation. #100

Closed Ravenex closed 5 years ago

Ravenex commented 5 years ago

I would like to request a method to define new properties of an object or class outside of the existing definition method by passing a properties object. This would be very useful in using ObjectModel in a dynamic way. For example:

const ObjectModel = require('objectmodel').ObjectModel

class Person extends ObjectModel({ name: String, _gender: [String] }) {

    loadDynamicProps(propGroup) {

        //this propList would be pulled from some outside dynamic definition such as JSON from a database
        propList = {
            sport: { type:String, value: "Baseball" },
            food: { type:[String] },
            number: { type: Number, value: 42 },
            _color: { type: [String], value: "Blue" }
        }

        this.defineProperties(propList) //the properties and values in propList would then be initialized
    } 

}

const Joe = new Person({ name: "Joe", _gender: "male" })
Joe.loadDynamicProps("favorites")
sylvainpolletvillard commented 5 years ago

Hello,

No need for a new method, all you need is Object.assign

const PersonModel = ObjectModel({ name: String, _gender: [String] }) 
​
class Person extends PersonModel {

    loadDynamicProps(propGroup) {
        const propList = {
            sport: String,
            food: [String],
            number: Number,
            _color: [String]
        }
​
        Object.assign(PersonModel.definition, propList)
    } 

}
​
const Joe = new Person({ name: "Joe", _gender: "male" })
Joe.loadDynamicProps("favorites")

If you also want to set the value of these properties, assign to this:

Object.assign(this, {
  sport: "Baseball",
  number: 42,
  _color: "Blue"
})

Note that you should probably declare this loadDynamicProps method in your model (PersonModel.loadDynamicProps) instead of in your instance. It's a bit weird that an instance is able to change its own model.

Ravenex commented 5 years ago

Thank you so much, I was not clear where or how to do that. This will work fine thanks.