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

Feature request: Allow $.param(function) #3008

Closed
jtrumbull opened this issue Mar 17, 2016 · 4 comments
Closed

Feature request: Allow $.param(function) #3008

jtrumbull opened this issue Mar 17, 2016 · 4 comments

Comments

@jtrumbull
Copy link

@jtrumbull jtrumbull commented Mar 17, 2016

Would it be a bad idea for $.param to allow a function argument?

var params = $.param(function(){
  var obj = {};
  // Dynamically build obj 
  return obj;
});

@dmethvin originally mentioned in #2633

I'm not a fan of adding more behavior to this API (and documenting it, don't forget). Unfortunately I suppose we've already crossed the Rubicon by calling functions at all, which prevent $.param() from being used as a way to passively serialize an object the way you would with JSON. But I'm still not a fan!

Although I am suggesting additional functionality, I tend to agree with @dmethvin 's point of minimizing the behavior of this function (I am a fan of simple and strict rules).

My thought process being, that if it's allowed on an attribute level, it should certainly be allowed at the root. However, secondarily, I am suggesting that this should only be allowed at the root, and to simply ignore method attributes (similar to JSON.stringify). To me, it would make more sense to pass a function that builds a dynamic object, than to pass an object that can build dynamic properties -almost seems like the tail is wagging the dog. A natural evolution might be to firstly allow this at the root, and to eventually phase out methods on an attribute basis.

@dmethvin
Copy link

@dmethvin dmethvin commented Mar 17, 2016

If it's only for the top level couldn't you just do this already?

var params = $.param((function(){
  var obj = {};
  // Dynamically build obj 
  return obj;
})());

@jtrumbull
Copy link
Author

@jtrumbull jtrumbull commented Mar 18, 2016

You can, and your example is essentially how I am handling it now. But I guess for the same reason you wouldn't want to do:

var params = $.param({
  a: (function(){ return 1; })(),
  b: (function(){ return 2; })(),
  c: (function(){ return 3; })()
});

..more key strokes, more bytes, uglier code. Probably the most relevant reason (for me) is dealing with ambiguous variables.

Consider the following example (which happens frequently in my world):

function Widget (options) {
  this.url = options.url;
  this.params = options.params; // Plain object -or method that returns object.
}

Widget.prototype.remoteFunction = function () {
  $.ajax({
    url: this.url,
    data: this.params // Could be object or method
  });
}

Simply invoking this.params would result in an error if an object was passed. So, I find myself adding abstraction:

data: typeof this.params === 'function' ? this.params() : this.params;

(might not seem like much, but this is repeated a few dozen times in each of my projects)

This seems to break the model of library (most abstract) to plugin (less abstract) to implementation (most defined). And, in my opinion, the most natural place for an ambiguous entry point would be the library.

On a lesser note:

function getParams () {
  return {};
}
$.ajax({ url: 'abc.php', data: getParams }); // Seems more intuitive than
$.ajax({ url: 'abc.php', data: getParams() });

Allowing a function argument would result in the same for both examples, but currently only the latter would work.

@gibson042
Copy link

@gibson042 gibson042 commented Mar 18, 2016

I'm opposed to expanding the input signature in this way, and with @dmethvin on valuing consistency between jQuery.param and JSON.stringify (i.e., if we call anything it should be as toJSON serialization methods). Come to think of it, I wonder how much of this we can deprecate in the 3.x release cycle.

@timmywil
Copy link

@timmywil timmywil commented Jun 30, 2016

I agree with Dave and Richard.

@timmywil timmywil closed this Jun 30, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants