“How” to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

前端 未结 11 1277
醉梦人生
醉梦人生 2020-11-28 19:45

I am well aware it can be done and I\'ve looked at quite a few places (including: Best practice for saving an entire collection?). But I\'m still not clear \"exactly how\"

11条回答
  •  -上瘾入骨i
    2020-11-28 20:15

    Here's a simple example:

    var Books = Backbone.Collection.extend({
    model: Book,
    url: function() {
      return '/books/';
    },
    save: function(){
      Backbone.sync('create', this, {
        success: function() {
          console.log('Saved!');
        }
      });
     }
    });
    

    When you call the save() method on your collection, it will send a PUT method request to the defined URL.

提交回复
热议问题