11

i need to make request on server that needs of particulary api key and i need to use the crud method tu update my model and as soon as...

For example i have this code in ajax to get element from server:

 function getapi() {

$.ajax({
    url: 'https://api.parse.com/1/classes/autolavaggi/QSfl*****',
    type: 'GET',
    dataType: 'json',

    success: function(obj) { 

        alert("nome autolavaggio "+obj.nome);

    },
    error: function() {
        alert('Errore');


    },
    beforeSend: setHeader
});
}  

    //GET GET  GET  GET  GET GET  GET  GET  Header Header Header Header
    function setHeader(xhr) {
xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
}

How can i do to assign this particular ajax call to crud method save,fetch or another??

Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96

1 Answers1

19

Each of the crud methods accept an options hash that will get forwarded to the ajax call. In the case of a collection fetch:

var Model = Backbone.Model.extend({});
var Collection = Backbone.Collection.extend({
  model: Model,
  url: 'https://api.parse.com/1/classes/autolavaggi/QSfl*****'
});

var setHeader = function (xhr) {
  xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
  xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
}

var collection = new Collection();
collection.fetch({ beforeSend: setHeader });

Alternatively, override sync:

var sync = Backbone.sync;
Backbone.sync = function(method, model, options) {
  options.beforeSend = function (xhr) {
    xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
    xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
  };

  // Update other options here.

  sync(method, model, options);
};
jmconrad
  • 629
  • 5
  • 4
  • Is this an example of override of method sync?? – Stefano Maglione Jul 14 '12 at 08:45
  • That was an example of manipulating the resulting ajax request without overriding sync. I updated the post to also include an example of globally overriding sync. – jmconrad Jul 14 '12 at 13:28
  • But in wich part of code(model,collection ecc.)can i write the override of sync?? – Stefano Maglione Jul 14 '12 at 19:24
  • If you want to do it globally so that it applies to all models and collections, override Backbone.sync. If you only want to it for an individual model or collection, override the sync method of that model or collection. – jmconrad Jul 14 '12 at 21:47
  • Just override Backbone.sync similar to what is above in a place that will get executed before any of your REST calls. – jmconrad Jul 15 '12 at 13:45
  • uhmm I don't understand, i want to override sync globally, but where can i write the code??In a model in a collection?? – Stefano Maglione Jul 17 '12 at 09:36
  • Probably too late, you can write it in your main.js if you got nowhere to put it! – dineth Nov 06 '12 at 06:16