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

Access current route vs params? #30

Open
timwis opened this issue Mar 2, 2013 · 0 comments
Open

Access current route vs params? #30

timwis opened this issue Mar 2, 2013 · 0 comments

Comments

@timwis
Copy link

timwis commented Mar 2, 2013

Backbone.history provides a property called fragment which is normally used to obtain the current fragment to, for example, build <a> links. I'd like to suggest backbone-query-parameters add to this by providing the current route/fragment and the current parameters as a separate property, ie.

#foo/bar?a=1&b=2 becomes {fragment: "foo/bar?a=1&b=2", route: "foo/bar", params: {a: 1, b: 2}}

The reason I'm suggesting this is because I have a method in my router called buildFragment that allows you to build a URL fragment based on the current params:

,buildFragment: function(route, add, preserve) { var newParams = preserve !== undefined && preserve.length && this.params !== undefined ? _.extend(add, _.pick(this.params, preserve)) : add; return (route || this.route) + ( ! _.isEmpty(newParams) ? "?" + $.param(newParams) : ""); }

This allows me to build a URL by adding a parameter and preserving any existing ones I'd like to keep.

For my case, I'm setting the route and params property manually on every route, but it would seem like this should be set automatically. Let me know if you know of a better way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant