scotttrinh/angular-localForage

$$hashKey is now being saved on setitem

bvmensvoort opened this issue · 8 comments

On a commit (6a42de3, line 125), the angular.copy() is removed in favour of angular.extend({}, value).
The commit message states the $promise value is still removed.
But the $$haskey property is still copied on arrays. Cause similar to mine

Can this be resolved within the plugin, or is a workaround more suitable for this?

Example:
value = [{$$hashKey: "object:70", Title:"Dummy"}, {$$hashKey: "object:71", Title:"Dummy2"}]

angular.copy(value) gives: [{Title:"Dummy"}, {Title:"Dummy2"}]

angular.extend({}, value) gives: [{$$hashKey: "object:70", Title:"Dummy"}, {$$hashKey: "object:71", Title:"Dummy2"}]

🤔 Interesting. Didn't anticipate that as an issue. Let me dig into the Angular internals and have it match the behavior of angular.copy for backwards compatibility. I'll try to get something up ASAP and let you know!

It looks like your example is slightly wrong:

var o = { $$hashKey: 'object:1' };
angular.copy(o); // {}
angular.extend(o); // { $$hashKey: 'object:1' }
angular.extend({}, o); // {}

So, for your case, I suspect your code just isn't getting into that if block to begin with (probably doesn't have a $promise value?). Can you confirm that you are getting in there? At any rate, I should move the extend call out of the if block so it will have the same behavior as before. Stand by!

@BMarinos
Check out #138 and let me know if seems right to you. If you want to test it in your app, you can use npm to install angular-localForage at the hash of that PR.

Thanks for the really quick solution!
It works as expected; It doesn't save the $$hashKey anymore. The change seems ok to me.
I am not sure if this will work with nested arrays and nested ng-repeats. But at this moment it is not an issue for me.

About your questions, the object is an array, so it wouldn't go in to the if block. Sorry, it is hard to reproduce. If needed I can check this.

Yeah, that's what I suspected. It's not that extend doesn't remove $$hashKey, it's that your case was never hitting extend and we were using copy right away before.

As far as nested arrays and repeats go, I feel like, for backwards compatibility, we should handle that issue, so I'll keep working on it. Expect a patch fix soon that removes $$hashKey recursively.

When I execute extend, it doesn't remove the $$hashKey. (I tested this in Chrome Developer Console)
JSON.stringify(localCopy); // "[{"$$hashKey":"object:70"},{}]"
angular.copy(localCopy); // [{}]
angular.extend({}, localCopy); // [{ $$hashKey: 'object:70' }]

And angular.copy works for nested as well:
angular.copy(JSON.parse('[{"array":[{"$$hashKey":"object:70"}]},{}]')); // [{"array":[{}],{}]

(And my variable got no $promise property, by the way.)

Looking forward to the patch.

Ahh. You're extending an array, which is not what we're trying to do here. The change I made already handles that, but there is a real problem there. Should have something today! Thanks for your patience, and sorry to anyone else watching this issue where this change broke their stuff!

Thanks for all the help @BMarinos ! New release published as 1.3.6.