Skip to content

Commit

Permalink
Added put as seperate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
janhommes committed Jun 7, 2017
1 parent 3b8c30c commit 9459d6a
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 39 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# o.js

_o.js beta v0.3.1_
_o.js beta v0.3.3_

o.js is a client side Odata Javascript library to simplify the request of data. The main goal is to build a **standalone, lightweight and easy** to understand Odata lib.

Expand Down Expand Up @@ -185,7 +185,7 @@ However, if you have set an endpoint you can still do a full endpoint request fo
```javascript
//basic config
o().config({
endpoint: null,
endpoint: null, // The default endpoint to use.
format: 'json', // The media format. Default is JSON.
autoFormat: true, // Will always append a $format=json to each query if set to true.
version: 4, // currently only tested for Version 4. Most will work in version 3 as well.
Expand All @@ -196,11 +196,7 @@ o().config({
headers: [], // an array of additional headers [{name:'headername',value:'headervalue'}]
username: null, // the basic auth username
password: null, // the basic auth password
isAsync: true, // set this to false to enable sync requests. Only usable without basic auth
isCors: true, // set this to false to disable CORS
openAjaxRequests: 0,// a counter for all open ajax request to determine that are all ready TODO: Move this out of the config
isHashRoute: true, // set this var to false to disable automatic #-hash setting on routes
appending: '' // set this value to append something to a any request. eg.: [{name:'apikey', value:'xyz'}]
isAsync: true // set this to false to enable sync requests. Only usable without basic auth
});
```

Expand Down
76 changes: 47 additions & 29 deletions o.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// +++
// o.js v0.3.1
// o.js v0.3.3
//
// o.js is a simple oData wrapper for JavaScript.
// Currently supporting the following operations:
// .get() / .post() / .put() / .delete() / .first() / .take() / .skip() / .filter() / .orderBy() / .orderByDesc() / .count() /.search() / .select() / .any() / .ref() / .deleteRef()
//
// By Jan Hommes
// Date: 06.06.2017
// Date: 06/07/2017
// Contributors: Matteo Antony Mistretta (https://github.com/IceOnFire)
//
// --------------------
Expand Down Expand Up @@ -339,7 +339,7 @@
// adds a inline count
// +++
base.inlineCount = function (countOption) {
if (oConfig.version == 4) {
if (base.oConfig.version == 4) {
countOption = countOption || 'true';
if (!isQueryThrowEx('$count')) {
addQuery('$count', countOption);
Expand Down Expand Up @@ -393,7 +393,7 @@
if (resource == null || resource.get) {
throwEx('You need to define a resource with the find() method to append an navigation property');
}
if (oConfig.version < 4) {
if (base.oConfig.version < 4) {
resource.method = 'POST';
resource.path.push('$link');
resource.path.push({ resource: navPath, get: null });
Expand All @@ -418,7 +418,7 @@
if (resource == null || resource.get) {
throwEx('You need to define a resource with the find() method to append an navigation property');
}
if (oConfig.version < 4) {
if (base.oConfig.version < 4) {
resource.method = 'POST';
resource.path.push('$link');
resource.path.push({ resource: navPath, get: null });
Expand All @@ -443,7 +443,6 @@

// +++
// This function actually queries the oData service with a GET request
// TODO: maybe add some pseudonyms...
// +++
base.get = function (callback, errorCallback) {
// init the q -> if node require a node promise -> if ES6, try ES6 promise
Expand Down Expand Up @@ -513,10 +512,9 @@
}

// +++
// does a update with the given Data to the current dataset
// always uses the PATCH http method
// does a update with the given Data to the current dataset with PATCH.
// +++
base.patch = base.put = function (data, res) {
base.patch = function (data, res) {

//add the resource
if (res) {
Expand All @@ -533,6 +531,26 @@
return (base);
}

// +++
// does a update with the given Data to the current dataset with PUT.
// +++
base.put = function (data, res) {

//add the resource
if (res) {
addNewResource(res);
}

if (!resource.path[resource.path.length - 1] || !resource.path[resource.path.length - 1].get)
throwEx('Bulk updates are not supported. You need to query a unique resource with find() to patch/put it.');

//set the method and data
resource.method = 'PUT';
resource.data = data;

return (base);
}

// +++
// does a delete with the given Data to the current dataset
// +++
Expand Down Expand Up @@ -565,7 +583,7 @@

var searchStr = buildSearchFilter(searchColumns, searchWord, searchFunc);

if (oConfig.version == 4 && isSupported) {
if (base.oConfig.version == 4 && isSupported) {
if (!isQueryThrowEx('$search')) {
addQuery('$search', searchStr, searchStr);
}
Expand Down Expand Up @@ -648,7 +666,7 @@
// builds a search filter
// ++++
function buildSearchFilter(searchColumns, searchWord, searchFunc) {
searchFunc = searchFunc || (oConfig.version == 4 ? 'contains' : 'substringof');
searchFunc = searchFunc || (base.oConfig.version == 4 ? 'contains' : 'substringof');
var searchWordSplit = searchWord.split(' ');
var isNotExactSearch = (searchFunc === 'contains' || searchFunc === 'substringof');

Expand All @@ -657,7 +675,7 @@
var wordArr = [];
if (isNotExactSearch) {
for (var m = 0; m < searchWordSplit.length; m++) {
if (oConfig.version == 4) {
if (base.oConfig.version == 4) {
wordArr.push(searchFunc + '(' + searchColumns[i] + ',\'' + searchWordSplit[m] + '\')');
}
else {
Expand Down Expand Up @@ -776,7 +794,7 @@
var routeRegex = routeStr;
if (!(routeStr instanceof RegExp)) {
//set the hash if needed
if (oConfig.isHashRoute && !startsWith(routeStr, '#')) {
if (base.oConfig.isHashRoute && !startsWith(routeStr, '#')) {
routeStr = '#' + routeStr;
}
//build up a regex
Expand Down Expand Up @@ -844,13 +862,13 @@
resource = res;

//add the default format
if (!isQuery('$format') && oConfig.autoFormat) {
if (!isQuery('$format') && base.oConfig.autoFormat) {
addQuery('$format', base.oConfig.format);
}

//appendings
for (var i = 0; i < oConfig.appending.length; i++) {
addQuery(oConfig.appending[i].name, oConfig.appending[i].value);
for (var i = 0; i < base.oConfig.appending.length; i++) {
addQuery(base.oConfig.appending[i].name, base.oConfig.appending[i].value);
}
}

Expand All @@ -876,36 +894,36 @@
//build a ajax request
var ajaxReq = createCORSRequest(resource.method, buildQuery());
//check if we only have one request or we need to force batch because of isXDomainRequest
if ((countMethod(['POST', 'PATCH', 'DELETE']) <= 1 && isSave) && !isXDomainRequest) {
if ((countMethod(['POST', 'PATCH', 'DELETE', 'PUT']) <= 1 && isSave) && !isXDomainRequest) {
startAjaxReq(ajaxReq, stringify(resource.data), callback, errorCallback, false,
[
{ name: 'Accept', value: 'application/json' },
{ name: 'Content-Type', value: 'application/json' }
],
param, resourceList[resourceList.length - 1].progress);
//because the post/put/delete is done, we remove the resource to assume that it will not be posted again
removeResource(['POST', 'PATCH', 'DELETE']);
// because the post/put/delete is done, we remove the resource to assume that it will not be posted again
removeResource(['POST', 'PATCH', 'DELETE', 'PUT']);
}
//do a $batch request
// do a $batch request
else {
//generate a uui for this batch
// generate a uui for this batch
var guid = generateUUID();

//build the endpoint
// build the endpoint
var endpoint = base.oConfig.endpoint + (endsWith(base.oConfig.endpoint, '/') ? '' : '/') + '$batch';

//appendings
for (var i = 0; i < oConfig.appending.length; i++) {
endpoint += (i === 0 ? '?' : '&') + oConfig.appending[i].name + '=' + oConfig.appending[i].value;
// appendings
for (var i = 0; i < base.oConfig.appending.length; i++) {
endpoint += (i === 0 ? '?' : '&') + base.oConfig.appending[i].name + '=' + base.oConfig.appending[i].value;
}

//start the request
// start the request
startAjaxReq(createCORSRequest('POST', endpoint), buildBatchBody(guid, isSave), callback, errorCallback, true,
//add the necessary headers
// add the necessary headers
[{ name: 'Content-Type', value: 'multipart/mixed; boundary=batch_' + guid }],
param, resourceList[resourceList.length - 1].progress);
if (isSave) {
//because the post/put/delete is done, we remove the resource to assume that it will not be posted again
// because the post/put/delete is done, we remove the resource to assume that it will not be posted again
removeResource(['POST', 'PUT', 'DELETE']);
}
}
Expand Down Expand Up @@ -1482,7 +1500,7 @@
function strFormat() {
var str = arguments[0];
var para = arguments[1];
for (p in para) {
for (var p in para) {
var regex = new RegExp(p, 'g');
if (typeof str === 'string')
str = str.replace(regex, para[p]);
Expand Down
Loading

0 comments on commit 9459d6a

Please sign in to comment.