Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. var computed = ko.computed(function() {
  2. readSomeObservables(); //<-- if these change, notify computed
  3. ko.stopCollectingDependencies();
  4. readSomeMoreObservables(); //<-- if these change, do not notify computed
  5. ko.resumeCollectingDependencies();
  6. });
  7.  
  8. window.setTimeout(function() {
  9. readSomeMoreObservables();
  10. }, 0);
  11.  
  12. // this one is updated
  13. // if any of the subscribeables used in readSomeMoreObservables changes
  14. // but that is hopefully cheap
  15. var tempComputed = ko.computed(function() {
  16. readSomeMoreObservables();
  17. });
  18.  
  19.  
  20. var computed = ko.computed(function() {
  21. readSomeObservables(); //<-- if these change, notify computed
  22.  
  23. // do not update on readSomeMoreObservables
  24. tempComputed.peek();
  25. });
  26.  
  27. var computed = ko.computed(function() {
  28. var a = readSomeObservables(); //<-- if these change, notify computed
  29. var b;
  30.  
  31. // capture any dependencies from readSomeMoreObservables
  32. // in a temporary computed, then immediately dispose
  33. // of it to release those captured dependencies
  34. ko.computed(function() { b = readSomeMoreObservables(); }).dispose();
  35.  
  36. return a + b; // or whatever
  37. });
  38.  
  39. it('Should not subscribe to subscribeables called by ignore', function() {
  40.  
  41.  
  42. var observableInner = ko.observable('initial'),
  43. observableOuter = ko.observable(),
  44. called = 0,
  45. computedInner = ko.computed(function() { return observableInner(); }),
  46. computedOuter = ko.computed(function() {
  47. called += 1;
  48. // read dependend
  49. observableOuter();
  50.  
  51. // read ignored
  52. var result = ko.dependencyDetection.ignore(computedInner, null)
  53. expect(result).toEqual('initial');
  54.  
  55. return true;
  56. });
  57.  
  58. expect(called).toEqual(1);
  59.  
  60. // update the one we are depending on
  61. observableOuter(1);
  62. expect(called).toEqual(2);
  63.  
  64. // update the inner one which should trigger an update to computedInner but not to computedOuter
  65. observableInner('ignore');
  66. expect(called).toEqual(2);
  67. });
  68.  
  69. var observableTwoInitialState = this.observableTwo();
  70.  
  71. this.computed = ko.computed(function() {
  72. return this.observableOne() + observableTwoInitialState;
  73. }, this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement