Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Handle batch object cookie read/write operations#123

Closed
johan wants to merge 4 commits intocarhartl:masterfrom
johan:master
Closed

Handle batch object cookie read/write operations#123
johan wants to merge 4 commits intocarhartl:masterfrom
johan:master

Conversation

@johan
Copy link

@johan johan commented Oct 28, 2012

This implements, tests and documents $.cookie() batch read/write operations:

$.cookie() // => { all: "cookie", values: "present" } – fixes #16

$.cookie({ all: null, values: "tweaked" }) // deletes "all" and edits "values"

$.cookie(obj, options); // batch op with a common options arg used for all

I also fixed the test suite to work right, even if run on a server you have other cookies on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return key here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reference

Aside 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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interest yes, time hardly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@carhartl
Copy link
Owner

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants