Commit 37ff22d0 authored by Sven Franck's avatar Sven Franck

merged put/post into storage.write method, cleanup old methods

parent ea43ce63
......@@ -1061,25 +1061,16 @@
* @param {object} obj Action Object
**/
"new": function (obj) {
storage.save(obj);
},
/**
* Add a server
* @method submit
* @param {object} obj Action Object
**/
"update_configuration": function (obj) {
storage.add(obj);
storage.write(obj);
},
/**
* Add a user to the storage
* Update an object
* @method submit
* @param {object} obj Action Object
**/
"add": function (obj) {
storage.add(obj);
"update": function (obj) {
storage.write(obj);
},
/**
......@@ -5528,23 +5519,36 @@
storage = {};
/**
* Generate a new object
* @method newStore
* Write to storage = POST or PUT
* @method write
* @param {object} obj Action object
*/
storage.save = function (obj) {
var form, data, valid, prefix, decode, id, answer, goto, type;
storage.write = function (obj) {
var form, data, valid, prefix, decode, id, answer, goto, config, method;
form = document.getElementById(obj.id);
type = obj.state.url_pointer[obj.element.getAttribute("data-action")];
prefix = obj.id + "_";
valid = storage.validate(obj);
data = storage.parseForm(valid, prefix, true);
// post or put
if (form.identifier) {
method = "put";
config = {
"_view": obj.state.view
};
data._id = form.identifier.value;
} else {
config = {
"call": obj.state.url_pointer[obj.element.getAttribute("data-action")]
};
}
RSVP.resolve(
app.storage_dict.items.post(data, {"call": type})
app.storage_dict.items[method || "post"](data, config)
)
.then(function (response) {
util.loader("", "status_dict.saved", "check");
if (obj.state.callback) {
answer = util.parse(response);
id = answer.id;
......@@ -5845,7 +5849,6 @@
} else {
replace = form_to_submit.id + "_";
obj = {};
// force portal_type
if (config.gadget.state.force_type) {
config.gadget.state.type = config.gadget.state.force_type;
......@@ -7068,7 +7071,7 @@
// set callback and pointer on state
pass.state.callback = pass.config.property_dict.submit_to;
pass.state.url_pointer = pass.config.property_dict.url_pointer;
pass.state.url_pointer = pass.config.property_dict.url_pointer || {};
// tuck to gadget
......@@ -7332,84 +7335,42 @@
}
}
// if we have form_data we continue
if (pass.form_data === undefined) {
// loop over item and create a JIO record
for (i = 0; i < pass.items.length; i += 1) {
record = pass.items[i];
obj = {};
for (key in record) {
if (record.hasOwnProperty(key)) {
if (validate) {
if (answer.response[key]) {
obj[key] = record[key];
} else {
util.error("store: field " + key +
" not defined for type = " + pass.type);
}
} else {
// loop over item and create a JIO record
for (i = 0; i < pass.items.length; i += 1) {
record = pass.items[i];
obj = {};
for (key in record) {
if (record.hasOwnProperty(key)) {
if (validate) {
if (answer.response[key]) {
obj[key] = record[key];
} else {
util.error("store: field " + key +
" not defined for type = " + pass.type);
}
} else {
obj[key] = record[key];
}
}
}
// add portal type
// obj.portal_type = pass.type;
// if an object identifier was set in the form, we PUT, else POST
if (obj.identifier) {
method = "put";
hacked_view = {"_view": pass.view};
obj._id = obj.identifier;
delete obj.identifier;
}
// add portal type
// obj.portal_type = pass.type;
// store
promises[i] = store[method || "post"](obj, hacked_view || {})
.then(function (answer) {
return answer;
})
.fail(util.error);
// if an object identifier was set in the form, we PUT, else POST
if (obj.identifier) {
method = "put";
hacked_view = {"_view": pass.view};
obj._id = obj.identifier;
delete obj.identifier;
}
} else {
console.log("SHOULD BE REDUNDANT")
// // do it again, Sam...
// promises[0] = jIO.util.ajax({
// "url": app.storage_dict.property_dict.items.storage_list[0].url,
// "type": "GET",
// "xhrFields": {
// "withCredentials": true
// }
// })
// .then(function(response) {
// return jIO.util.ajax({
// "url": util.parse(response.target.responseText)._links.me.href,
// "xhrFields": {
// "withCredentials": true
// }
// });
// })
// .then(function (answer) {
// return jIO.util.ajax({
// "url": util.parse(answer.target.responseText)._actions[pass.fetch].href,
// "type": "POST",
// "data": pass.form_data,
// "xhrFields": {
// "withCredentials": true
// }
// });
// })
// .then(function (erp5_response) {
// header = erp5_response.target.getResponseHeader("Location");
// return RSVP.resolve({
// "status": 200,
// "foo": "bar",
// "location": header
// });
// })
// .fail(util.error);
// store
promises[i] = store[method || "post"](obj, hacked_view || {})
.then(function (answer) {
return answer;
})
.fail(util.error);
}
return RSVP.all(promises)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment