如何在主干中指定 url 和 header 以在我的模型上使用 crud 方法? [英] How to specify url and header in backbone to use crud method on my model?
本文介绍了如何在主干中指定 url 和 header 以在我的模型上使用 crud 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在需要特定 api 密钥的服务器上发出请求,我需要使用 crud 方法更新我的模型,并尽快...
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...
例如我在ajax中有这个代码来从服务器获取元素:
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*****************');
}
如何将这个特定的 ajax 调用分配给 crud 方法 save、fetch 或其他方法??
How can i do to assign this particular ajax call to crud method save,fetch or another??
推荐答案
每个 crud 方法都接受一个选项哈希,该哈希将被转发到 ajax 调用.在集合获取的情况下:
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 });
或者,覆盖同步:
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);
};
这篇关于如何在主干中指定 url 和 header 以在我的模型上使用 crud 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文