What is backbone’s convention/best practice for retrieving data from a RESTful web service, based on data entered in a form?
From what I understand I can register a click observer in the view, so when the form submit button is clicked the view will retrieve the data from the form. But I’m unclear on whether I execute the AJAX call from the view, model, or router.
Essentially, I’m unclear as to which of the following is convention:
- Have the view get the parameters, execute the AJAX call and then pass the returned JSON to the model
- Have the view get the parameters and pass them to the model and have it execute the AJAX call and retrieve the data from the web service
- Have the view get the parameters and pass them to a router and have it execute the AJAX call and populate the appropriate model and view accordingly?
NOTE: I need to submit search criteria to a RESTful Web Service, which expects the data to be POSTed; correct me if I’m wrong, but it doesn’t seem like I can use: fetch or save. Should I create a function within the model that uses $.ajax({…}) to post the params and receive the JSON data?
The backbone.js models are already setup to do RESTful requests. Method 2 is what you want to do.
The only thing you have to setup in the model is a URL: