sylvainpolletvillard / ObjectModel

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

Problem with extended Definitions #45

Closed Mojo90 closed 7 years ago

Mojo90 commented 7 years ago

What I get from your Docs:

for object models, the resulting definition is the deep (recursive) merge of the definition objects

Now if I do:

var Model = require("objectmodel");

var Address = new Model({
  address: {
    company: [String],
    name: String,
    street: String,
    zip: String,
    city: String,
    country: String
  }
});

var Customer = Address.extend({
  id: String
});

Customer.create = function(properties) {
  return new Customer(properties);
};

var obj = Customer.create({<Object>});
console.dir(obj.constructor.definition);

It prints out:

{ address: 
   { company: [ [Function: String] ],
     name: [Function: String],
     street: [Function: String],
     zip: [Function: String],
     city: [Function: String],
     country: [Function: String] 
  }
}

For me id is missing here in the definition as I understand from docs or did I get sth. wrong?

sylvainpolletvillard commented 7 years ago

Right, it looks like a bug. Basically when extending a model, all properties from the parent prototype are copied : https://github.com/sylvainpolletvillard/ObjectModel/blob/master/src/model.js#L78

The bug here is that it also includes the constructor property, that's why you get obj.constructor === Address and not Customer. But the Customer definition is fine, see console.log(Customer).

If I am right, this should be a one-line fix to skip the constructor property. I will do it tonight.

sylvainpolletvillard commented 7 years ago

fixed in 2.6.2 thanks for the report :+1:

Mojo90 commented 7 years ago

You are right but as I use it now as a Data Model for my backend I have only the object in my classes and not the Customer Class itself. So great for the fast fix, thank you!