Handle batch object cookie read/write operations#123
Handle batch object cookie read/write operations#123johan wants to merge 4 commits intocarhartl:masterfrom
Conversation
There was a problem hiding this comment.
If you followed the flow of the code, key here is the object of key: value pairs you just set, so you can use it just as any other assignment in javascript, e g: a = b = { x: 1, y: 2 }; sets both a and b to the given object. If you insert a $.cookie call anywhere in that assignment chain, this makes it still work, which is expected, intuitive and useful behaviour.
There was a problem hiding this comment.
I know what the value of key is, but I disagree about it being "expected and intuitive". Quite the contrary. More like unexpected, undocumented, inconsistent and encouraging bad coding patterns.
$.cookie('a', 'aaa', options); // returns undefined
$.cookie({a: 'aaa', b: 'bbb', options); // returns pairs object by referenceAside from it making the return value inconsistent based on invocation signature, it is somewhat counter-intuitive. Why would it return the pairs object? I'd expect maybe a boolean return on whether the action was successful, or maybe the expiration date of the set cookies, or perhaps the newly formed cookie-syntax string. But not the pairs object.
All that just to save a literal assignment?
var values = $.cookie({a: 'aaa', b: 'bbb'}, options);
// Instead of:
var values = {a: 'aaa', b: 'bbb'};
$.cookie(values, options);There was a problem hiding this comment.
Since the issue here is the batch cookie operation I think this return change should be threated in another PR.
The ´$.cookie(name, value)´ returns the string representing the cookie and it is used in a few tests, I don't think that is a good idea either, the plugin should return undefined if there is no documented feature specifying what it should return...
There was a problem hiding this comment.
It is documented higher up in the pull request. You are all welcome to make your own pull requests based on this one changing as much as you like to whatever you personally deem more useful or intuitive. Personally, my opinion stands as is, and also, I don't plan to have computerized net access for most of the month of December anyway, so little will change here.
That said, I don't believe the repository maintainer has the time or interest in accepting pull requests; this and other good patches have been sitting untouched rather long.
(Yes. The whole pull request is to save myself and others writing this assignment loop and storing away the key:val pairs object outside of $.cookie.)
There was a problem hiding this comment.
And regarding this PR in particular, I am still reluctant to add it, since I'd like to see whether there is greater popular demand, otherwise I'd like to keep the codebase as light as possible.
|
Thanks for the PR. I'm now closing it, since batch reading is now part of the cookie plugin and I haven't seen demand for batch writing yet. |
This implements, tests and documents $.cookie() batch read/write operations:
I also fixed the test suite to work right, even if run on a server you have other cookies on.