javascript Backbone.js 应该从 URL 中获取 GET 参数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11838089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:30:01  来源:igfitidea点击:

Should Backbone.js grab GET parameters from URL?

javascriptjquerybackbone.jsunderscore.js

提问by Nyxynyx

I am trying to implement a search function for my website. When the user types a search term foobarinto a inputbox and submits it, he is redirected to http://mydomain.com/search?query=foobar.

我正在尝试为我的网站实现搜索功能。当用户foobarinput框中键入搜索词并提交时,他将被重定向到http://mydomain.com/search?query=foobar

Problem::How should I grab the GET parameters queryfrom the URL, and send it to the backend and get a array of results back as a JSON response? Should I even do it this way?

问题:我应该如何query从 URL 中获取 GET 参数,并将其发送到后端并以 JSON 响应的形式返回结果数组?我应该这样做吗?

My current attempt below does not even cause the searchfunction to be triggered.

我目前在下面的尝试甚至search不会触发该功能。

Router

路由器

var AppRouter = Backbone.Router.extend({
    routes: {
        'search?query=:query': 'search'
        // ... and some other routes
    },

    search: function(query) {
        this.photoList = new SearchCollection();
        var self = this;
        this.photoList.fetch({
            data: {query: query},
            success: function() {
                self.photoListView = new PhotoListView({ collection: self.photoList });
                self.photoListView.render();
            }
        });
    }

});

var app = new AppRouter();
Backbone.history.start({
    pushState: true,
    root: '/'
});

采纳答案by tbranyen

There have been several issues filed against Backbone for this very issue. There is an existing plugin that works well for this:

已经针对这个问题针对 Backbone 提出了几个问题。有一个现有的插件可以很好地解决这个问题:

https://github.com/jhudson8/backbone-query-parameters

https://github.com/jhudson8/backbone-query-parameters

Alternatively, I'm currently using query string parameters in a mock API that matches Backbone's route matching. Looks something like this

或者,我目前正在模拟 API 中使用查询字符串参数,该 API 与 Backbone 的路由匹配匹配。看起来像这样

Route

路线

"/api/v2/application/:query"

"/api/v2/application/:query"

Query

询问

application: function(query) {
  var params = $.deparam(query.slice(1));
  // params.something...
}

As to your actual issue at hand how are you redirecting to index.htmlto support pushState?

至于您手头的实际问题,您如何重定向index.html到支持pushState

回答by Gerard

I hit this same issue and contemplated using backbone-query-parameters, but that should be considered generally an incorrect approach.

我遇到了同样的问题并考虑使用主干查询参数,但这通常应该被认为是一种不正确的方法。

The url query string is not meant for the front end. They get sent to the server and force a refresh when navigating from page.html to page.html?something=something.

url 查询字符串不适用于前端。它们被发送到服务器并在从 page.html 导航到 page.html?something=something 时强制刷新。

You should be using hash fragments instead. i.e. http://www.example.com/ajax.html#key1=value1&key2=value2then just get those values the normal backbone way and build your request params from that.

您应该改用哈希片段。即http://www.example.com/ajax.html#key1=value1&key2=value2然后只需以正常的主干方式获取这些值并从中构建您的请求参数。

See https://github.com/jashkenas/backbone/issues/891, https://developers.google.com/webmasters/ajax-crawling/docs/specification, http://tools.ietf.org/html/rfc3986#section-3.5

https://github.com/jashkenas/backbone/issues/891https://developers.google.com/webmasters/ajax-crawling/docs/specificationhttp://tools.ietf.org/html/rfc3986 #section-3.5

回答by ericbae

You can always read the URL via jQuery URL plugin. It works well.

您始终可以通过 jQuery URL 插件读取 URL。它运作良好。

https://github.com/allmarkedup/jQuery-URL-Parser

https://github.com/allmarkedup/jQuery-URL-Parser

回答by Claudiu Hojda

There are very few cases when you need to read the URL and extract the GET params. I think that you are doing things wrong and here are my options:

在极少数情况下,您需要读取 URL 并提取 GET 参数。我认为你做错了,这是我的选择:

1) if you are having just one page in your app (single app page) you can display results as they type in your inputfield or after they hit submit

1)如果您的应用程序中只有一页(单个应用程序页面),您可以在他们在您的input字段中输入或点击后显示结果submit

2) if you are redirecting the user to a different page that means you can bootstrap dataso that after the page is loaded backbonewill just have to render your results and only make other requests if you change your search word

2)如果您将用户重定向到不同的页面,这意味着您可以引导数据,以便在页面加载后backbone只需要呈现您的结果,并且只有在您更改搜索词时才发出其他请求

3) you can have a javascriptvariable which is initialized on page load directly from the server where working with GET params is probably easier

3)你可以有一个javascript变量,它在页面加载时直接从服务器初始化,在那里使用 GET params 可能更容易