angular-localForage icon indicating copy to clipboard operation
angular-localForage copied to clipboard

Unbind unexpected behavior

Open martinbrylski opened this issue 10 years ago • 1 comments

I've a scope variable that I'm watching at. Based on this variable I bind to a localForage key. In case of change, I want to unbind the 'old' value and bind the 'new'.

The current unbind method will delete the data stored for this key, what is not the intended behavior (the opposite of bind())

Either I'm wrong with my use-case and the usage of this library or unbind has a strange behavior.

To overcome my issue i currently I replaced the unbind method with:

LocalForageInstance.prototype.unbind = function unbind($scope, opts) {
        if(angular.isString(opts)) {
          opts = {
            key: opts
          }
        } else if(!angular.isObject(opts) || angular.isUndefined(opts.key)) {
          throw new Error("You must define a key to unbind");
        }

        var defaultOpts = {
          scopeKey: opts.key,
          name: defaultConfig.name
        };

        // If no defined options we use defaults otherwise extend defaults
        opts = angular.extend({}, defaultOpts, opts);

        var self = lfInstances[opts.name];

        if(angular.isUndefined(self)) {
          throw new Error("You must use the name of an existing instance");
        }

        // $parse(opts.scopeKey).assign($scope, null);
        if(angular.isDefined(watchers[opts.key])) {
          watchers[opts.key](); // unwatch
          delete watchers[opts.key];
        }
        return $q.when(true);
      };

martinbrylski avatar Jul 06 '15 13:07 martinbrylski

To preserve backwards compatibility, I will keep the current behavior of unbind, but we can pass another argument to the method that will decide whether to remove the value from the DB or not. Does that sound reasonable to you, @martinbrylski or anyone else who might want this behavior changed?

scotttrinh avatar Jun 19 '16 22:06 scotttrinh