angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

Doc request: $new vs $create vs $build vs $buildRaw

Open scriby opened this issue 10 years ago • 4 comments

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

scriby avatar Sep 16 '14 18:09 scriby

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.

iobaixas avatar Sep 16 '14 18:09 iobaixas

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?

scriby avatar Sep 16 '14 18:09 scriby

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?

iobaixas avatar Sep 16 '14 19:09 iobaixas

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 });
};

scriby avatar Sep 16 '14 19:09 scriby