Giter VIP home page Giter VIP logo

Comments (10)

sylvainpolletvillard avatar sylvainpolletvillard commented on May 18, 2024 1

Hi,

defaults is a convenient method for setting things in the prototype, so you could write the code this way:

var FileInfo = Model({
	name: String,
	created: [Date, Number]
});
FileInfo.prototype.name = "Untitled file";
FileInfo.prototype.created = Date.now()

As you can see it is simply a value assignment, so there is no way to tell if you want to assign a reference to the function itself or if you want this function to be invoked to get the value.

Moreover, as the default value is defined in the prototype, it is shared by all instances of the model. Definitely not what you want.

So for your usecase, I would suggest to use a factory function to init your models (see "How do I declare a constructor function to be called on instanciation before validating my model ?" here http://objectmodel.js.org/#common-questions)

Another way would be to replace the created property by a getCreationDate function, so that you can assign function(){ return Date.now() } as default for this property. But I'm not sure it suits your needs if you have other type checking rules in mind for this date value.

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on May 18, 2024 1

Okay, I may have found another way to do it with a getter/setter:

var FileInfo = Model({
	name: String,
	created: [Date, Number]
});
Object.defineProperty(FileInfo.prototype, "created", { 
   get: function(){ return this._created || (this._created = new Date()); },
   set: function(val){ this._created = val; }
})

var f = new FileInfo({ name: "file" });
console.log(f.created); // Fri Feb 03 2017 11:43:28 GMT+0100 (Paris, Madrid)
f.created = "test" // TypeError: expecting created to be Date or Number, got String "test"

from objectmodel.

joseandrespg avatar joseandrespg commented on May 18, 2024

Hi!

The factory solution would be the one that do the job for this case. But at this point would be a big change for us that we might need to agree. And yes, we want to keep the type checking on these fields.

Letting you know our context, we are migrating from mongoose schemas to objectmodel, we have a basic script to automate the definition of the models and avoid duplication, and one of the missing features was this one ("created" dates mainly).

Thanks a lot!

from objectmodel.

rubtk avatar rubtk commented on May 18, 2024

Have a same problem. By using constructor method, have not got how it would work with ArrayModel. Array model dont care about user defined "constructor". I would try go second way.

from objectmodel.

rubtk avatar rubtk commented on May 18, 2024

Nop, this dont work with second method as well. How to solve this problem with ArrayModel's? I think there must be good feature to have some "contructor" feature for ArrayModel.

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on May 18, 2024

@rubtk Could you open another issue with some code please ?

from objectmodel.

rubtk avatar rubtk commented on May 18, 2024

@sylvainpolletvillard Solution with prototypes worked finally (there was a bug with debugger breakpoints).
But still, i dont want to use it, coz this looks very creepy for my purposes. I have an invalid input JS object, those must be pre-edited before creating model. Would be nice feature to set somewhere object model constructor for array models, to automatically construct it inside library. Should i open issue for this?

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on May 18, 2024

If you need to do a "pre-editing" thing before creating the model instance, I strongly advise you to declare a factory function for that. This is the cleanest approach as everything is explicit, there is no creepy magic involved :

MyModel.create = function(...args){
   args = doSomePreEditingThingWith(...args);
   return new MyModel(...args);
}

Remember than ArrayModels are supposed to be a transparent layer over regular Arrays, and that regular arrays do not have these custom constructors. Same for object models and regular objects. Only classes have a constructor that could be used for this purpose.

from objectmodel.

rubtk avatar rubtk commented on May 18, 2024

Thanks for help. I understand and agree with you. But problem is i get array of invalid object's, so now "by design" i do foreach of this array before creating array model. Does ArrayModel already iterates this source array or not? Or this is real "transparent layer", so technically there are no place to put user preprocessing? I declared factory function for single model instance, so your advice is made this for ArrayModel and move proprocessing logic there? Does i understood you right?

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on May 18, 2024

You should open another issue with your code so that we can discuss your specific problem.

from objectmodel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.