Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

angular.copy and angular.equals don't play well with MobX #15533

Closed
@thorn0

Description

@thorn0

Do you want to request a feature or report a bug?

feature

What is the current behavior?

In theory, it should be easy to start working with MobX. It will be fun they said. Just replace plain objects and arrays with observables and the existing code shouldn't notice much difference. Just a couple of tweaks might be needed. Unfortunately, it's not the case with Angular 1.x apps. Watchers get broken when they are passed observable arrays.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (template: http://plnkr.co/edit/tpl:yBpEi4).

$watch ignores changes in observable arrays:
http://plnkr.co/edit/eNBaj5HBvvGdd6PAos8g?p=preview

$watch uses angular.copy and angular.equals internally, and it's these functions that don't work with MobX:

var observableArray = mobx.observable([1, 2, 3]);
var copy = angular.copy(observableArray);
console.log(angular.equals(observableArray, copy)); // true
observableArray.push(100);
console.log(angular.equals(observableArray, copy)); // true!
// `copy` is completely unusable:
copy[1]; // TypeError
copy.length; // TypeError

What is the expected behavior?

Would be great to have a way to teach angular.copy and angular.equals new object types.

What is the motivation / use case for changing the behavior?

I decided to play with MobX in my app to see whether it can make the state management simpler. As the first step, I was going to just replace my model/state objects with observables to see what changes are needed.

Which versions of Angular, and which browser / OS are affected by this issue? Did this work in previous versions of Angular? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.

Angular 1.6, 1.5

Other information (e.g. stacktraces, related issues, suggestions how to fix)

This issue may sound like #10096, but it's not about specifying an alternative copy and equals implementation on every call to $watch. A way to override this logic globally for all the watchers, directive bindings, etc. is needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions