Reputation: 51
In my application, I would like to create a search form in order to retrieve some users. This form contains many criteria (lastname, firstname, birthdate, etc.). But I do not understand how I can post my form in order to filter my collection. I can not use fetch method because if I use GET method, my search criteria will be displayed in the URL. In fact I would like to submit a POST request (which contains the search criteria serialize in JSON) to the server and retrieve a user list.
I have read the backbone documentation but I do not found anything that allow me to do that. This functionality is not supported by backbone ? There is some workarround to implement this requirements ?
Thanks in advance
Upvotes: 1
Views: 697
Reputation: 5703
Tim is correct that this isn't exactly what Backbone is meant for. That being said I think I would still at least use a Backbone model to track the state of the search criteria. On this model I would add a custom method for activating search that would serialize the model's state and POST it to the server itself. On the success of this method, I would use the results to reset the current user collection.
Here is my idea in code:
var SearchModel = Backbone.Model.extend({
defaults: {
firstName: "*",
lastName: "Smith"
},
search: function() {
$.ajax({
dataType: "json",
type: "POST",
data: this.toJSON(),
success: function(data) {
this.options.peopleCollection.reset(data);
}
})
}
});
var myPeopleCollection = new PeopleCollection();
var mySearch = new SearchModel({ peopleCollection: myPeopleCollection });
mySearch.set({ firstName: "Steve"});
mySearch.set({ lastName: "Smith" });
mySearch.search();
Upvotes: 0
Reputation: 3318
In short, I don't think backbone is a good fit for what you are describing. Read on for a longer version...
Based on your question, I think backbone would be well suited to display your collection of users, but not so good at the submission of your filter criteria.
You could create a criteria model and set the attributes of the model to Username, Birthday, etc and then you could post it to the server using backbone. However when you save it using backbone, it is assumed that you saved the criteria. Any result that comes back from the server from that POST is assumed to be updated properties of the criteria, and not a list of users.
Upvotes: 1