我正在开发一个带有路由器的主干应用程序,我在从url中获取查询参数时遇到了一个问题,我没有找到任何直接的方法来解决这个问题。
我有一个类似test.com/main/page1?state=code&value=blaa的url
我想用主干路由器的方式从url中获取state和value的值。请帮我找到一个解决方案。
发布于 2014-07-04 03:19:34
他们已经为你创建了Execute来做一些很棒的事情,比如在路由被调用之前解析查询字符串。更多信息:http://backbonejs.org/#Router-execute
发布于 2014-07-03 18:16:26
@Prats:可以,您可以通过以下方式获取中间页(非主页) url参数值。
router.js
backboneApp.Routers.MyrouterRouter = Backbone.Router.extend({
routes:{
"" : "routeToList",
"register" : "routeToRegister",
//Here the actual url along with the fragment(parameters)
"edit/:id" : "routeToEdit"
},
.
.
.
//Other functions.
.
.
.
//sending the url fragment to render() function....
routeToEdit: function(id)
{
this.editView = new backboneApp.Views.EditView();
//Note, this render function from the corresponding view,
//can receive the extra fragment(parameter)
this.editView.render(id);
this.$content.html(this.editView.el);
}
});edit.js
backboneApp.Views.EditView = Backbone.View.extend({
.
.
.
// some other functions....
.
.
.
render: function(id)
{
// The id which I passed received here, from here you can
// manipulate as you wanted.
});
}
});注意:我使用url作为edit/:id,用于路由。根据您的需要,您可以在routes方法中使用您的url,方法是使用constant-url/: variable-fragment,以便将此variable-fragment传递给相应视图的render()函数。在那里,您可以随心所欲地操作参数。多个参数也是可能的,如果你遵循上面相同的概念,但它需要更多的努力。希望这对你有帮助。
https://stackoverflow.com/questions/24547793
复制相似问题