-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($location): add queryString method #2701
Conversation
Added new queyString method to locationProvider to allow for custom query string parsers.
I quite like this idea but I think it needs to be thought through carefully as it may allow people to easily break their applications. |
@petebacondarwin @joshrtay while I also like the addition but shouldn't it become a regular service? It seems to have 2 methods on it: |
@petebacondarwin since the parser has to be set explicitly, shouldn't it be the dev's job to make sure it doesn't break their application? |
@pkozlowski-opensource setting the query string parser changes the behavior of $location. It seems to me that methods that change the way a service behave are placed on the service provider. queryString expects a parser, which is an object with a parse function and a strinfigy function. JSON and qs are valid parsers. |
On 20 May 2013 17:08, joshrtay [email protected] wrote:
|
@petebacondarwin - can you give me an example of what your proposing? I'm not sure I follow. |
@petebacondarwin you are overprotecting. There're a lot of other similar ways to "break" AngularJS, e.g. by using IMHO, this options provides additional freedom, and given sane default of |
@kstep - don't worry! I never said this shouldn't be merged. In fact you'll see that I thought it was quite a good idea. I just said that we need to think this through carefully to make sure that it is a sensible addition and won't break anything. As an example of providing this as a service. I could imagine something like this:
|
@petebacondarwin how is this approach safer than setting the parser directly on the locationProvider? Ultimately the user is still changing the behavior of $location. This approach would offer some advantages, if angular shipped with a couple query parsers that the user could then select in the config block. |
@joshrtay - it doesn't. It just is an example of what Pawel was suggesting. Pete
|
@joshrtay @petebacondarwin - just to clarify - I wasn't suggesting service-based approach to make it "safer" - but rather to make this service reusable. I can see other places where it could potentially fit, $resource and $http being ones that come to my mind. In those services we need to parse objects to query strings as well. Anyway, this was just a quick suggestion, I don't feel strongly about it. Once again, I'm just noticing that there are other parts of AngularJS where we do need to manipulate query strings (converting to objects and back) |
@pkozlowski-opensource @petebacondarwin so your suggesting a service like: angular.module('ng').factory('$queryString', function() {
var parser = {stringify: toKeyValue, parse: parseKeyValue};
return function(p) {
if (isDefined(p))
parser = p;
else
return parser;
};
}); that $resource, $location, $http and dev code could use. I like this approach better. If you guys think it is sufficiently "safe" i'd be happy to implement it. |
@joshrtay yeh, this is roughly what I was talking about. We could have a default implementation that behaves as it does today and people could override it as needed. But once again, this was just a quick thought, I will let others to weight here. |
For the |
@chirayuk Yes, but it would mean that someone would interact with different types of back-ends in one app... Not sure how common this might be but you've got a valid point here. In this case we should have ability so specify a custom de-serialization service (function?) per resource. But yeh, I don't know how common this might be as an use-case... |
Actually I think it is quite common. Consider OAuth, where you might log On 11 July 2013 21:46, Pawel Kozlowski [email protected] wrote:
|
I think we should close this PR and look at creating a PR with a more generalized solution, which would need to be overridable on a URL by URL basis. I expect we would provide one or two built in serializers that confirm to a specified interface that people can implement with their own versions. These built-in serializers would be exposed as services and there would be hooks in things like $location, $http and $resource for specifying which of these serialization services to use and when. |
Is there an open thread for this? #3213 is closed also. |
I second @blowsie 's question - I too have been scraping the interwebs for a reasonable means of shaping querystring as described by @petebacondarwin |
Its gone awfully quiet here, any open tickets for this? |
last @petebacondarwin proposal I think fits best in some scenarios, but adds extra complexity by introspecting into the URL to choose the serializer. |
Added new queyString method to locationProvider to allow for custom query string parsers. The method expects an object with a stringify function for encoding and a parse function for decoding.
For example you could use node-querystring:
or JSON: