Эта функция использует библиотеки jquery и Dojo для получения информации из Sharepoint.Я хочу использовать только Dojo.Как я могу перенести часть jquery в Dojo?
Если быть точным, вызов $ .ajax необходимо преобразовать в dojo.xhrPost
$.ajax({
url: url,
type: "POST",
async: false,
dataType: "xml",
data: getSOAPEnvelope(fields, guid),
complete: function (xData, status) {//parse xml to json},
contentType: "text/xml"
})
Это моя попытка:
dojo.xhrPost({
url: url,
sync:true, //in jquery, sync => async
handleAs:"xml", //in jquery, handleAs => dataType
content:getSOAPEnvelope(fields, guid),//in jquery, content => data
load: (fn from 1.1 section goes here) //in jquery, load => complete
headers: {Content-Type:"text/xml"} //in jquery is contentType:"text/xml"
}
});
И этополная оригинальная (не перенесенная) функция:
/*
Returns a json object from a sharepoint list
url and guid arguments are strings (without the {})
fields is an optional array of fileds to be output. this will reduce teh size of the response.
uniqueFields is an optional array of fileds to be filtered. For example:
if the list has
row,fruit,color,size
1,banana,green,small
2,banana,yellow,small
3,apple,yellow,medium
4,apple,green,small
5,apple,red,small
for a list of fruits, you put: getJsonItems(url,guid,true,['row',fruit','color'],['fruit']) -> returns rows 1 and 3
for a list of fruit sizes, do: getJsonItems(url,guid,true,false,['fruit','size']) -> returns all columsn for items (1,3,4)
*/
dojo.require("dojox.xml.parser");
function getJsonItems(url, guid, fields, uniqueFields) {
//prepare the SOAP envelope
var jsonItems = []
var pkHash = []
//1. fetch xml data from server and parses into json
$.ajax({
url: url,
type: "POST",
async: false,
dataType: "xml",
data: getSOAPEnvelope(fields, guid),
complete:
//1.1 when data is received from the server, parse it to json
function (xData, status) {
//1.1.1 convert xml to DOM for easier manipulation
var dom = dojox.xml.parser.parse(xData.responseXML.xml)
//1.1.2 get xml rows
var rows = dom.getElementsByTagName("z:row")
//1.1.3 parse each xml row and add it to jsonItems array
dojo.forEach(rows, function (row, i) {
var jsonItem = {};
var uniqueKey = "";
//1.1.3.1 parse each xml row into json object. (It removes the ";#" prefix and other MS junk for lookup values or values with spaces)
for (var j = 0; j < row.attributes.length; j++) {
//1.1.3.1.1 parse the col name by removing ows_, replacing hex numbers (x_00xx_) and trimming spaces
var col = row.attributes[j].nodeName.replace("ows_", "").replace(/_x([A-F\d]{4})_/gi, function (str, hexNum) { return eval("\"\\u" + hexNum + "\"") }).replace(/^\s+|\s+$/g, "$1")
var val = row.attributes[j].nodeTypedValue.replace(/\d+;#/, "")
//1.1.3.2 add property to jsonItem
if (dojo.indexOf(fields?fields:[col], col) >= 0) jsonItem[col] = val;
//1.1.3.3 filter duplicates based on uniqueFields
if (dojo.indexOf(uniqueFields?uniqueFields:[col], col) >= 0) uniqueKey += col + val;
}
//1.1.3.3 add to uniqueKey to pkHash
if (dojo.indexOf(pkHash, uniqueKey) < 0) {
pkHash.push(uniqueKey);
jsonItems.push(jsonItem)
}
})
},
contentType: "text/xml"
})
//2. return parsed xml to json ojbect
return jsonItems
}
function getSOAPEnvelope(fields,guid){
//prepare ViewFields element
var viewFields = "";
if (fields) {
viewFields = "<viewFields><ViewFields Properties='True'>"
dojo.forEach(fields, function (e) {viewFields += "<FieldRef Name='"+e+"'/>"});
viewFields += "</ViewFields></viewFields>";
}
var envelope = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>{" + guid + "}</listName>\
" + viewFields + " \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";
return envelope;
}