I've been looking through multiple posts about how to save a Backbone collection using a non-RESTful server and I'm still a little confused. I've created a collection where I've overridden the toJSON
method to customise my data for posting to my API ("/api/entity/735/request/personDelete"
currently swapped out for jsfiddles /echo/json). So I've created a save
method which uses Backbone.sync
, on success
I'm logging out any kind of response and the object is empty, not sure where things get lost or what I'm doing wrong; can anyone give me some guidance? Would just like to get this example working so I can use something like this going forward.
JS
var PersonCollection = Backbone.Collection.extend({
model: PersonModel,
url: function() {
// Dummy JSFiddle endpoint
// Example non-RESTful url "/api/entity/735/request/personDelete"
return '/echo/json/';
},
/**
* Override toJSON to loop through collection models making
* custom objects containing specific attributes to be posted.
*/
toJSON: function() {
console.log(this.models);
var plucked = this.models.map(function(model) {
return _.pick( model.toJSON(), ["id","name", "teams"] )
});
console.log(plucked);
return plucked;
},
save: function(options) {
Backbone.sync('create', this, {
success: function(data, textStatus, jqXHR) {
console.log('Saved!', data);
}
});
}
});
JSFiddle: http://jsfiddle.net/kyllle/f1h4cz7f/3/
There isn't a save game option yet, it's either a commitment or casual experience for the time being. As for closing the game, just hold the (Esc) key down until it quits to the main menu.
Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).
BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications. When a model changes, it automatically updates the HTML of your application. BackboneJS is a simple library that helps in separating business and user interface logic.
Advertisements. Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.
You don't have to force yourself to use sync
if it doesn't help you. sync
is there to save you time in common scenarios.
As you can see in the annotated sync
code, it eventually just calls jQuery.ajax and includes logic to help with RESTful backends.
Also it triggers some events, which you might or might not listen in other parts of your app, like request
(when the request was made) and sync
(when the request was successfully completed), or error
(if the request failed)
All of these you can do from your app, if reinventing sync
isn't exciting.
Prepare your data, call $.ajax
to send the data to your backend, and optionally trigger
the backbone events, if you're going to listen to them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With