angular-restmod
angular-restmod copied to clipboard
Doc request: $new vs $create vs $build vs $buildRaw
I'm hoping we can get some docs comparing the various ways of constructing models and when to use them. Also, I'm curious if I can use the built-in javascript new keyword or if it's off-limits.
Thanks,
Chris
Using new Model(id)
will work just fine to create a new record. The only consideration is that it will ALWAYS create a new record, not like $new
that future extensions are allowed to override to provide some caching strategies.
I've added your request to the next milestone so I can track it.
Couldn't you make it such that new Model(id)
behaved the same as Model.$new(id)
? (Because in JS if you return an object from a constructor it overrides the default return value.)
Or, did you want them to behave differently?
Not sure on how I could accomplish that, I could use an additional 'type' and do something like:
function Model(id) {
return Model.$new(id)
}
Model.$new = function(id) {
return new ModelRecord(id);
};
But then a record wouldn't be an instance of Model, maybe that is not so important, what do you think?
Maybe something like this...
function Model(id, params) {
if (params == null || !params.instantiate) {
return Model.$new(id);
} else {
//normal model construction
}
}
Model.$new = function(id) {
return new Model(id, { instantiate: true });
};