Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does bind() a two way binding? #129

Open
Puresoft opened this issue Sep 22, 2016 · 4 comments
Open

does bind() a two way binding? #129

Puresoft opened this issue Sep 22, 2016 · 4 comments
Milestone

Comments

@Puresoft
Copy link

If I bind to the same key in different controllers and one controller modify the object, this
modification is not reflected in the other controller right?

Wouldn't it be nice to have it that way.?

@scotttrinh
Copy link
Owner

Yeah, I imagine people would expect it to work like that. A kind of "three-way" binding where changes in the scope update the DB and also update any other scope properties that are bound to the same key.

Just took a quick peek at the implementation (I didn't write it...) and it might take a little bit of work to accomplish, but seems like a good candidate for the next major version. I'd argue that this is a breaking change since people might be depending on the current behavior. Maybe we can make this an opt-in feature.

Stay tuned, and thanks for bringing this up!

@scotttrinh scotttrinh added this to the 2.0.0 milestone Sep 22, 2016
@imwhocodes
Copy link

How it can be done with the current relase?
I tried as @Puresoft suggest, the data is update in the local $scope of the controller but the "storage" is not updated

@scotttrinh
Copy link
Owner

@imwhocodes
You could set up the notifications like this (from the README.md):

angular.module('yourModule', ['LocalForageModule'])
.config(['$localForageProvider', function($localForageProvider){
    $localForageProvider.setNotify(true, true); // itemSet, itemRemove
}]);

And then in your controller, you could listen for changes like:

$scope.$on('LocalForageModule.setItem', function onSetItem(item) {
  // item === {key: key, newvalue: value, driver: localforage.driver};
  $scope.key = item.value;
});

Having said that, I would recommend against using angular-localForage as a bridge between different controllers, honestly. It's definitely a hidden coupling between controllers, and if two controllers need to share data, the typical Angular way to do this is with a service.

@scotttrinh
Copy link
Owner

scotttrinh commented Jun 1, 2017

Actually, that won't keep direct mutations in sync, just when using setItem. 🤔 Not sure that there is currently a way to do that, but I'll dive a little deeper. Actually, bind uses setItem under the hood, so that should work. I don't personally use bind, so I'm a bit of a newb when it comes to how it works. Sorry for the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants