Should Backbone.js grab GET parameters from URL?

I am trying to implement a search function for my site. When a user enters a search query foobarinto a field inputand sends it, he is redirected to http://mydomain.com/search?query=foobar.

Problem :: . How can I grab the GET parameters queryfrom a URL and send it to the server and get an array of results back as a JSON response? Should I do it like this?

My current attempt below does not trigger the function search.

the 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: '/'
});
+5
source share
4 answers

For this same problem, several problems with Backbone were filed. There is an existing plugin that works well for this:

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

, API, . -

"/api/v2/application/:query"

Query

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

, index.html pushState?

+5

, URL- GET. , - , :

1), ( ), input , submit

2), , , backbone ,

3) javascript, , GET, ,

-1

All Articles