29

I have managed to work with REST API's to fetch() data where the urls contain minimal parameters (and use GET).

How would one retrieve a collection through a POST request?

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
mike
  • 8,041
  • 19
  • 53
  • 68

4 Answers4

63

Also note that fetch supports Jquery.ajax parameters, so you can easily set type = post in the call.

Messages.fetch({data: {api_key: 'secretkey'}, type: 'POST'});

For more parameters: http://api.jquery.com/jQuery.ajax/

Arvid Janson
  • 1,034
  • 10
  • 10
3
try {
    // THIS for POST+JSON
    options.contentType = 'application/json';
    options.type = 'POST';
    options.data = JSON.stringify(options.data);

    // OR THIS for GET+URL-encoded
    //options.data = $.param(_.clone(options.data));

    console.log('.fetch options = ', options);
    collection.fetch(options);
} catch (excp) {
    alert(excp);
}
2

You may need to extend the Collection object to install your own convention for fetches. In doing so, you would likely provide your own fetch function. Something like:

fetch : function(options) {
  options || (options = {});
  var model = this;
  var success = function(resp) {
    if (!model.set(model.parse(resp), options)) return false;
    if (options.success) options.success(model, resp);
  };
  var error = wrapError(options.error, model, options);
  (this.sync || Backbone.sync)('create', this, success, error);
  return this;
}

where it uses a 'create' instead of a 'read'. On first blush, this is what I'd try first, though there may be a more elegant way to do it.

The downside of this approach is that you essentially have framework code in your app and if the framework changes you might encounter problems. You would do well to compartmentalize this change into a separate layer to make it easy to update with new framework releases.

Bill Eisenhauer
  • 6,183
  • 2
  • 30
  • 28
  • though, I didn't go with this route, it did help me figure out that I could create a sync method for the collection specifically so thanks and +1 ! – mike Jun 21 '11 at 18:21
  • Well, thanks. Given that only one line in the above is changing and its the line feeding sync, it seems like writing a custom sync is the way to go. I had not realized that one might have different syncs in the same app-space, but it now makes sense. Having had my mind opened to that possibility, I'm much better than an upvote for having tried to answer this. – Bill Eisenhauer Jun 21 '11 at 22:57
2

Backbone.sync is the function used to interact with the server via your models. You can provide your own implementation that issues a POST request for the 'read' method instead of GET. See http://documentcloud.github.com/backbone/#Sync

Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
  • I had originally tried to take this approach. I didn't want to overwrite Backbone.sync for my entire application... Then I realized that I could give the collection it's own sync method so I went that route. Thanks.. – mike Jun 21 '11 at 18:19