ghidoz / angular2-jsonapi

A lightweight Angular 2 adapter for JSON API
200 stars 123 forks source link

Refactoring: Use plain old javascript objects as data models #34

Open reva2 opened 7 years ago

reva2 commented 7 years ago

In current implementation data models highly coupled with data storage. I think it will be better if we will use POJO augmented with decorators for data models. I.e. all methods that change data in underlying storage (currently, save()) should be moved from JsonApiModel to JsonApiDataStore.

Typical interaction with data storage can look like:

// create record
let author = new AuthorModel();
storage.saveRecord(author);

// updating record
storage.findRecord(BookModel, '1').subscribe(
  (book) => {
    book.title = "New title";
    storage.saveRecord(book);
  }
);

// deleting record
storage.findRecord(BookModel, '1').subscribe(
  (book) => storage.deleteRecord(book);
);

This approach have following advantages:

storage.beginTransaction();

/* posts: BlogPostModel[] */
posts.forEach((post) => {
  post.published = true;
  storage.saveRecord(post);  // request will be delayed because of transaction
});

storage.commit(); // update all posts in underlying storage using single request
HennerM commented 7 years ago

I also think it's a good idea to move the logic to the JsonApiDataStore. Removing the now needed inheritance of JsonApiModel of the data models will make it much easier to deal with the models in forms and components.

Tinostarn commented 7 years ago

Any idea on when this modification could be released ? :) Like said in #49, I can't use any application state container like ng2-redux, because I can't store my models as they contain some properties injected by JsonApiModel and some circular references that cannot be passed into JSON.stringify

joaogarin commented 7 years ago

I think this is a great idea, right now its very tricky to properly test components and services as bringing in the models is not so trivial and very couple with the librarie's data storage like mentioned, Would love to know if there are some plans on moving to something like this. I know that might mean some breaking changes, but maybe thats ok at this stage.

ZeevKatz commented 6 years ago

Something new about this issue?

xaun commented 5 years ago

This would be great. Quite a lot of extra working writing unit tests I've discovered, and also, Tinostarn's comment on ng2-redux.

Any plans to implement?