Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone: Saving an entire collection to non-RESTful server

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/

like image 657
styler Avatar asked Oct 14 '15 20:10

styler


People also ask

How can I save my backbone?

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.

What is Backbone in programming?

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).

How Backbone js works?

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.

What is collections in Backbone JS?

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.


1 Answers

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.

like image 165
Yura Avatar answered Sep 28 '22 12:09

Yura