Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Exception on dynamically generated list of nums #641

Closed
chalin opened this issue Feb 27, 2014 · 4 comments
Closed

Exception on dynamically generated list of nums #641

chalin opened this issue Feb 27, 2014 · 4 comments
Milestone

Comments

@chalin
Copy link
Contributor

chalin commented Feb 27, 2014

Consider the simple HTML test file below with a single line ctrl.list = {{ ctrl.list }} and a very small controller exporting List get list => new List.generate(3, (i) => i).

Run under Angular 0.9.8 it yields:

ctrl.list = [0, 1, 2]

Run under Angular from git yields the same output but with

Exception: Observer reaction functions should not change model. 
These watch changes were detected: ctrl.list: [0, 1, 2] <= [0, 1, 2]; [[ctrl.list = {{ ctrl.list }}]](ctrl.list): [[0, 1, 2]] <= [[0, 1, 2]]
These observe changes were detected:  (package:angular/core/scope.dart:521)

My guess is that this is related to the new change detection algorithm.
Is this the next expected behavior?


<!DOCTYPE html>
<html>
<head>
<title>Angular test</title>
</head>
<body test>
  <p>ctrl.list = {{ ctrl.list }}</p>
  <script type="application/dart">
    import 'package:angular/angular.dart';
    void main() { ngBootstrap(module: new Module()..type(MyController)); }

    @NgController(publishAs: 'ctrl', selector: '[test]')
    class MyController {
      List get list => new List.generate(3, (i) => i);
    }
    </script>
  <script src="packages/browser/dart.js"></script>
</body>
</html>
@chalin
Copy link
Contributor Author

chalin commented Feb 27, 2014

Oh, maybe this is related to #633?

@pavelgj
Copy link
Contributor

pavelgj commented Feb 27, 2014

Yes, under the new change detection this is not allowed. The list getter returns a new list every time and produces a lot of garbage. This pattern is detrimental to application performance.

Instead, you can do:

class MyController {
  List list;

  MyController(MyService service) {
    service.onDataChanged.listen((_) {
      _updateData();
    });
  }

  _updateData() {
    list = new List.generate(3, (i) => i);
  }
}

@vicb vicb self-assigned this Mar 3, 2014
@vicb vicb added this to the v0.9.9 milestone Mar 3, 2014
@vicb vicb removed their assignment Mar 3, 2014
@mhevery
Copy link
Contributor

mhevery commented Mar 7, 2014

works as expected.

@chalin
Copy link
Contributor Author

chalin commented Mar 20, 2014

@pavelgj or @mhevery : could you point me to a fully worked out example of the approach you promote above (using MyService etc)? Maybe a test case? Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants