I've rewritten a grails application so that its 3 domains are each available at what I believe are (mostly) correct/Restful URLs. Each controller for those domains has a single index action, and depending on which request.method is used and whether the id is passed, I have it ready to send off json replies or do save/delete/etc.
Within the gsp side of things, I have jquery, backbone, and underscore all loaded. I've double-checked, these load correctly and the respective APIs seem to be available from any code I stick into a script element.
However, despite this, I seem unable to make Backbone.js do anything useful at all. I've read through the documentation, at least a dozen tutorials/examples, and about 100 backbone-related questions here on Stackoverflow. The only thing that I seem certain of is that the Router functionality is of little interest in this particular project.
I've been told that I have a choice of whether to use DataTables or jqGrid as the grid widget for this application. I have no preferences either way, but if one is easier than the other considering Backbone.js, I'll happily choose it. I have had some luck loading data into Datatables but only by writing it in into the GSP table directly.
I am aware that Backbone might not be the best choice for this, but I have little choice in the matter. To be quite honest, I'm not exactly sure what Backbone.js is trying to achieve.
What is the simplest, most minimal code that will tie Backbone and the REST stuff together, such that when a button is clicked in the row of the grid widget, that the relevant record is deleted? So that the relevant data is loaded into the grid widget in the first place, too, for that matter? So that I can add/insert records from arbitrary html inputs in the bottom row of the grid, or to edit existing records listed there?
I can supply whatever code is needed, but it didn't seem needed... I think I have the Grails side down pretty close to what is required, and thought it would only clutter up the page. Apologies in advance for the rambling, trying to refine as I write it into a question you guys have a chance of answering.
Define the url
-attribute for each model and collection to represent the RESTful url that corresponds to that model or collection. This can be either just a string or a function that returns a string:
url: 'path/to/my/collection-or-model' // string
url: function() { // function
return 'type-of-collection-or-model/' + this.identifyingAttribute
}
Now you have the trail to your backend complete. Next up is CRUD (create, read, update, delete) -time!
model.save() // Saves your model, if it is new this will be a POST request, otherwise PUT
model.fetch() // Retrieves your model from the backed, this will be a GET request
model.destroy() // Destroys your model and removes it from the backed as well, a DELETE
you can also fetch entire collections with the collection.fetch()
-function. The best place to get accurate information about Backbone and CRUD operations is backbonejs.org, if you want a more example-rich tutorial I would refer to the CRUD portion of the great Wine-Cellar tutorial for Backbone.
Hope this helps!