Comments (10)
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.
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.
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.
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.
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.
@rubtk Could you open another issue with some code please ?
from objectmodel.
@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.
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.
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.
You should open another issue with your code so that we can discuss your specific problem.
from objectmodel.
Related Issues (20)
- Optional array model cause duplication in other instances of a model with default HOT 3
- Feature Request: Code Suggestion/IntelliSense on IDE (Jetbrain Webstorm) HOT 4
- Package objectmodel has been ignored because it contains invalid configuration HOT 2
- Problem Extending Typescript Class HOT 5
- Consider creating ADL serializer to generate ObjectModel? HOT 1
- How to nest SealedModels? HOT 8
- How to add custom methods to a Model? HOT 1
- Working with VSCode Intellisense HOT 2
- Question: Is it possible to skip more detailed checks if initial type check fails? HOT 6
- [email protected]: The engine "node" is incompatible with this module HOT 3
- Default for Nested Models HOT 1
- Load Json to Object HOT 1
- Model composition and defaults HOT 11
- Typescript expected number of arguments when using defaultTo() HOT 4
- get objectmodel added to typia benchmarks HOT 4
- Nice Project HOT 1
- How can i return a « cleaned » object ? HOT 2
- Use in Typescript ? HOT 2
- pre/post process some datas ? HOT 2
- I can't catch the exceptions HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objectmodel.