简体   繁体   English

NgRx 数据服务更新删除 object 原型,而不是更新更改的字段

[英]NgRx Data service Update removes object prototype instead of updating the changed fields

When my NgRx DefaultDataService updates, it removes the type/prototype of the objects in the store.当我的 NgRx DefaultDataService 更新时,它会删除存储中对象的类型/原型。 The objects are no longer of type Todo, but are simple objects with no prototype.这些对象不再是 Todo 类型,而是没有原型的简单对象。

The objects come from the server as json objects conforming to the Todo interface .对象来自服务器作为符合 Todo接口的 json 对象。 I pipe them to be entered into the store as Todo objects like this:我 pipe 将它们作为 Todo对象输入到存储中,如下所示:

@Injectable()
export class TodosDataService extends DefaultDataService<ITodo> {
    baseUrl;

constructor( http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
    super("Todos", http, httpUrlGenerator);
}

getAll(): Observable<Todo[]> {

    //HERE - the ITodo[] from server is mapped to Todo[], then saved to store
    return super.getAll().pipe(
        map(todosList => todosList.map(todoData => new Todo(todoData)))
    );
}

But piping them on this service's update() override method doesn't work:但是在此服务的 update() 覆盖方法上管道它们不起作用:

update(updatedTodo: Update<Todo>): Observable<Todo> {
    return super.update(updatedTodo).pipe(map(data => new Todo(data)));
}

Note: I've also tried this using my own httpClient.put request manually without DefaultDataService's super call and several other ways.注意:我也尝试过手动使用我自己的 httpClient.put 请求,而不使用 DefaultDataService 的超级调用和其他几种方式。

The problem problem is that when ngrx applies the deltas (changes) to the object in the store, it somehow not a todo object anymore .问题在于,当ngrx 将增量(更改)应用于商店中的 object 时,它在某种程度上不再是待办事项 object Here's a screenshot of the console right after running the above statement:这是运行上述语句后控制台的屏幕截图:

Todo Prototype is gone Todo 原型已消失

This is causing unexpected behavior.这会导致意外行为。

TL;DR: How can we make sure the update does not remove the prototype? TL;DR:我们如何确保更新不会删除原型? If that's not possible, is there a simple way to intercept updates before they hit the store, in order to provide custom implementation?如果这不可能,是否有一种简单的方法可以在更新到达商店之前拦截更新,以提供自定义实现?

This behavior is intended see the following GitHub issue for more info.此行为旨在查看以下 GitHub 问题以获取更多信息。 https://github.com/ngrx/platform/issues/1641 https://github.com/ngrx/platform/issues/1641

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM