0

After executing get or put in indexeddb, the callback is updating the scope. The problem is, no update is being triggered in the ui!

A common solution is to use apply or digest but that is wrong, you should NOT use those operations. Angular should update it automatically.

Now my guess, after reading some stuff, is that the callback is being executed in a different context, outside of the scope.

My question is basically: how can I execute an indexeddb callback in an angularjs scope context?

EDIT: A rough look on how its built:

GetObjectStoreData(iDb, objectStore, function (res) {
    $scope.result = res;
}

The call is made from inside the scope so the scope is used in the callback. The callback parameter is the contents of the object store

Sharon Dorot
  • 542
  • 1
  • 4
  • 15

1 Answers1

1

You asserted that you should not have to use Scope.$apply because AngularJS should do it for you.

This is true only for callbacks managed by Angular. For example, when you use $timeout in place of window.setTimeout, angular wraps your provided callback in a call to Scope.$apply, causing a digest cycle to run once your callback completes. If you interact directly with browser APIs rather than calling through AngularJS, it is your responsibility to call Scope.$apply at the appropriate time.

If you don't want to manage the scope directly, you could instead use a wrapper library like angular-indexedDB, which (amongst other things) handles the scope notifications when callbacks occur.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138