backbone-query-parameters icon indicating copy to clipboard operation
backbone-query-parameters copied to clipboard

Access current route vs params?

Open timwis opened this issue 11 years ago • 0 comments

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.

timwis avatar Mar 02 '13 19:03 timwis