troyanskiy/ngx-resource

Initialize Resource without HTTP call?

mimon opened this issue · 9 comments

mimon commented

Thank you for this library. I really liked ng-resource that existed in Angular 1 and I'm exited to explore this Angular 2 version of it.

From the current Readme example:

// Creating the news
    let newNews:INews = {
      date: '17.06.2016',
      title: 'The great day',
      text: 'The best day ever',
      fullText: 'Should be full text here';
    }
    // That will execute the POST request to https://domain.net/api/users
    // Expected to receive created news object which will be assigned to newNews
    let newNews = this.newsRes.save(newNews);

From this example it seems I have to POST newNews on the server in order to instantiate the resource object.

Is there any way I can instantiate a Resource object (with or without model data) without invoking any HTTP?

In Angular 1's $resource one could do

ExampleResource = $resource(...)
o = new ExampleResource({ model_fields: .. }) // I can now store and modify data in `o` before I POST/PUT
o.$save() // Results in a HTTP POST

Hello @mimon
Thank you for using the lib
I think you can do that with model
Please check doc how to create model and then you can do following

const myModel = this.myResource.$createModel();
myModel.field1 = 'data1';
myModel.$save();
mimon commented

Thanks. Hm, would it be possible to set data using a JSON object or does it have to be field by field ?

myModel.$setData(yourJsonObject)

It simply does Object.assign

You can overwrite the method to have custom data assignment

mimon commented

Hm, it seems I have to use myModel.$resource.$setData(...) (I'm inheriting ResourceCRUDBase). Does that make a difference or is that expected?

mimon commented

Hm, no I used ResourceCRUDBase since I'd like to avoid defining a model on the front-end (my model is quite large). I also have the use case that a resource may be identified by multiple keys. Eg. /shops/:shopId/products/:productId.

So I have

interface IKeys {
  shop_id: number;
}

@Injectable()
@ResourceParams({
  url: `/shops/{!shopId}/products/{id}`,
  params: {
    shopId: '@shop_id' // Products has a 'shop_id' field'
  }
})
export class ProductService extends ResourceCRUDBase<IKeys, IKeys, any, any> {

}

@mimon Unfortunately it's possible to have creation, $save() and etc only by creating Model.
Otherwise, you can only resource methods and work with pure js objects (without $save()...)

mimon commented

I was having a look at the example you suggested: https://github.com/troyanskiy/ngx-resource#example-of-resource-model-usage and tried to create a test app based on it.

But there seem to be some inconsistency there. It looks like

  • TestResource is depending on TestModel and vice verse:
  • TestModel is depending on TestResource

Is that an error or is it just me messing something up?

Hello all.

I've released some kind of beta of new library which is called rest-core + rest-ngx.
Please check them rest-core and rest-ngx.

It works the way like ngx-resource but has a lot of breaking changes.
Something was removed or simplified.

I've migrated all my projects to the new library.
If you wish to switch to HttpClient or use some other http handlers like fetch try to migrate your projects to the lib.

Bugs and help requests are welcome in corresponding repos.

Thanks!

PS: Since ngx-resource is no longer supported by me, closing the issue.