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

Extra hint for Multiplier Filter in "Explore More", chapter 5 #34

Open
zymsys opened this issue Jan 17, 2014 · 7 comments
Open

Extra hint for Multiplier Filter in "Explore More", chapter 5 #34

zymsys opened this issue Jan 17, 2014 · 7 comments

Comments

@zymsys
Copy link

zymsys commented Jan 17, 2014

I'm trying to do the Multiplier Filter at the end of chapter 5. The challenge is to write a filter which lets a user enter a multiplier, and have item quantities multiplied by that value on output.

My first attempt failed because the filter was modifying the original recipe instead of a copy for output, so values would keep multiplying over and over again. I tried having the filter construct new recipe models and return a List of those, but that threw it into a loop involving Scope / $watchCollection resulting in an error:

5 $digest() iterations reached. Aborting!
Watchers fired in the last 3 iterations: [["recipe in ctrl.recipes | orderBy:'name' | filter:{name:ctrl.nameFilterString} | categoryfilter:ctrl.categoryFilterMap | multiplierfilter:ctrl.multiplier ","ctrl.tooltipForRecipe(recipe)","recipe.rating","rating","FN: eval(scope) {\n try {\n return _expression.eval(scope);\n } on EvalError catch (e, s) {\n throw e.unwrap("$this", s);\n }\n }","ctrl.tooltipForRecipe(recipe)","recipe.rating","rating","FN: eval(scope) {\n try {\n return _expression.eval(scope);\n } on EvalError catch... (this catch bit repeats many times)

STACKTRACE:

0 Scope._digestWhileDirtyLoop (package:angular/core/scope.dart:466:5)

1 Scope.$digest (package:angular/core/scope.dart:415:28)

2 _autoDigestOnTurnDone (package:angular/core/scope.dart:108:14)

3 _rootRun (dart:async/zone.dart:688)

4 _ZoneDelegate.run (dart:async/zone.dart:417)

5 NgZone._finishTurn (package:angular/core/zone.dart:97:21)

6 NgZone._onRunBase (package:angular/core/zone.dart:57:20)

7 _onRunUnary (package:angular/core/zone.dart:66:22)

8 _ZoneDelegate.runUnary (dart:async/zone.dart:426)

9 _CustomizedZone.runUnary (dart:async/zone.dart:631)

10 _BaseZone.runUnaryGuarded (dart:async/zone.dart:546)

11 _BaseZone.bindUnaryCallback. (dart:async/zone.dart:572)

This repeats indefinitely until I stop it. It seems that having the filter return a different list causes it to re-evaluate the ng-repeat, and this goes on forever.

I'm pretty new to Dart and loving it, but I'd really appreciate an extra hint as to how I can better approach this. I've got my current stab at it in this gist: https://gist.github.com/zymsys/8479765

@chalin
Copy link
Contributor

chalin commented Jan 26, 2014

Hi. I am having similar issues ("5 $digest() iterations reached. Aborting!") though my approach was to write a filter that applied to the ingredient list only when it is displayed. I posted a request for more information about the permitted behavior of custom filters on Stackoverflow a few days ago. I posted a follow up on the angular-dart list. Have you been able to make any progress on this?

@zymsys
Copy link
Author

zymsys commented Jan 26, 2014

No, I haven't come back around to this yet. I completed the rest of the AngularDart tutorial without any trouble. I'll post here if I do figure it out.

@kwalrath
Copy link
Contributor

Newly answered in http://stackoverflow.com/questions/21322969/angulardart-custom-filter-call-method-required-to-be-idempotent, and the page now links to that explanation.

@chalin
Copy link
Contributor

chalin commented Jan 29, 2014

Hi Kathy. Did you notice my second comment to Misko's answer? IMHO, his solution only works if quantity is represented as a double; which means users would get to see multiplied quantities like "2.33333333333 cups of flour" :). In my final solution I had planned to use a special numeric class to handle fractional quantities more nicely. In such a case, even Misko's solution fails to work (at least, according to Misko, until this makes it into Angular Dart). Maybe a less controversial exercise could be proposed at the end of that chapter.

@kwalrath
Copy link
Contributor

Nope, I missed that. What exercise would you propose?

@kwalrath kwalrath reopened this Jan 29, 2014
@chalin
Copy link
Contributor

chalin commented Jan 29, 2014

It seems like we are left with choosing a filter that doesn't create new object instances (that aren't identical()). I cannot think of a (semi-)realistic example. The only thing that comes to mind for now is to suggest filtering on the quantity or the ingredient description.

@zymsys
Copy link
Author

zymsys commented Jan 29, 2014

Thanks, I was able to make it work by filtering only the ingredient. I didn't bother parsing out the quantity or dealing with fractions - I just used a placeholder like in my gist. Going back to the gist (https://gist.github.com/zymsys/8479765) I still want to figure out how it mutated the model. By returning a new array to display I didn't think it had done so. I'll take some more time with it and see if I can sort it out.

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

3 participants