Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
E
ecommerce-ui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
ecommerce-ui
Commits
ec79a5cb
Commit
ec79a5cb
authored
Feb 25, 2014
by
Sven Franck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added new storages
parent
c53908f0
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
295 additions
and
0 deletions
+295
-0
js/libs/jio/URI.js
js/libs/jio/URI.js
+77
-0
js/libs/jio/erp5storage.js
js/libs/jio/erp5storage.js
+218
-0
No files found.
js/libs/jio/URI.js
0 → 100644
View file @
ec79a5cb
This diff is collapsed.
Click to expand it.
js/libs/jio/erp5storage.js
0 → 100644
View file @
ec79a5cb
/*
* Copyright 2013, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
// JIO ERP5 Storage Description :
// {
// type: "erp5"
// url: {string}
// }
/*jslint indent: 2, nomen: true, unparam: true */
/*global jIO, complex_queries, console, UriTemplate, FormData, RSVP, URI */
(
function
(
jIO
,
complex_queries
,
URI
)
{
"
use strict
"
;
function
ERP5Storage
(
spec
)
{
if
(
typeof
spec
.
url
!==
'
string
'
&&
!
spec
.
url
)
{
throw
new
TypeError
(
"
ERP5 'url' must be a string
"
+
"
which contains more than one character.
"
);
}
this
.
_url
=
spec
.
url
;
}
ERP5Storage
.
prototype
.
_getSiteDocument
=
function
()
{
return
jIO
.
util
.
ajax
({
"
type
"
:
"
GET
"
,
"
url
"
:
this
.
_url
,
"
xhrFields
"
:
{
withCredentials
:
true
}
}).
then
(
function
(
response
)
{
return
JSON
.
parse
(
response
.
target
.
responseText
);
});
};
ERP5Storage
.
prototype
.
_get
=
function
(
param
,
options
)
{
return
this
.
_getSiteDocument
()
.
then
(
function
(
site_hal
)
{
return
jIO
.
util
.
ajax
({
"
type
"
:
"
GET
"
,
"
url
"
:
UriTemplate
.
parse
(
site_hal
.
_links
.
traverse
.
href
)
.
expand
({
relative_url
:
param
.
_id
,
view
:
options
.
_view
}),
"
xhrFields
"
:
{
withCredentials
:
true
}
});
})
.
then
(
function
(
response
)
{
var
result
=
JSON
.
parse
(
response
.
target
.
responseText
);
result
.
_id
=
param
.
_id
;
return
result
;
});
};
ERP5Storage
.
prototype
.
get
=
function
(
command
,
param
,
options
)
{
this
.
_get
(
param
,
options
)
.
then
(
function
(
response
)
{
command
.
success
({
"
data
"
:
response
});
})
.
fail
(
function
(
error
)
{
console
.
error
(
error
);
// XXX How to propagate the error
command
.
error
(
"
not_found
"
,
"
missing
"
,
"
Cannot find document
"
);
});
};
ERP5Storage
.
prototype
.
post
=
function
(
command
,
metadata
,
options
)
{
return
this
.
_getSiteDocument
()
.
then
(
function
(
site_hal
)
{
var
post_action
=
site_hal
.
_actions
.
add
,
data
=
new
FormData
(),
key
;
for
(
key
in
metadata
)
{
if
(
metadata
.
hasOwnProperty
(
key
))
{
// XXX Not a form dialog in this case but distant script
data
.
append
(
key
,
metadata
[
key
]);
}
}
return
jIO
.
util
.
ajax
({
"
type
"
:
post_action
.
method
,
"
url
"
:
post_action
.
href
,
"
data
"
:
data
,
"
xhrFields
"
:
{
withCredentials
:
true
}
});
}).
then
(
function
(
doc
)
{
// XXX Really depend on server response...
var
uri
=
new
URI
(
doc
.
target
.
getResponseHeader
(
"
Location
"
));
command
.
success
({
"
id
"
:
uri
.
segment
(
2
)});
}).
fail
(
function
(
error
)
{
console
.
error
(
error
);
command
.
error
(
500
,
"
Too bad...
"
,
"
Unable to post doc
"
);
});
};
ERP5Storage
.
prototype
.
put
=
function
(
command
,
metadata
,
options
)
{
return
this
.
_get
(
metadata
,
options
)
.
then
(
function
(
result
)
{
var
put_action
=
result
.
_embedded
.
_view
.
_actions
.
put
,
renderer_form
=
result
.
_embedded
.
_view
,
data
=
new
FormData
(),
key
;
data
.
append
(
renderer_form
.
form_id
.
key
,
renderer_form
.
form_id
[
'
default
'
]);
for
(
key
in
metadata
)
{
if
(
metadata
.
hasOwnProperty
(
key
))
{
if
(
key
!==
"
_id
"
)
{
// Hardcoded my_ ERP5 behaviour
if
(
renderer_form
.
hasOwnProperty
(
"
my_
"
+
key
))
{
data
.
append
(
renderer_form
[
"
my_
"
+
key
].
key
,
metadata
[
key
]);
}
else
{
throw
new
Error
(
"
Can not save property
"
+
key
);
}
}
}
}
return
jIO
.
util
.
ajax
({
"
type
"
:
put_action
.
method
,
"
url
"
:
put_action
.
href
,
"
data
"
:
data
,
"
xhrFields
"
:
{
withCredentials
:
true
}
});
})
.
then
(
function
(
result
)
{
command
.
success
(
result
);
})
.
fail
(
function
(
error
)
{
console
.
error
(
error
);
command
.
error
(
"
error
"
,
"
did not work as expected
"
,
"
Unable to call put
"
);
});
};
ERP5Storage
.
prototype
.
allDocs
=
function
(
command
,
param
,
options
)
{
return
this
.
_getSiteDocument
()
.
then
(
function
(
site_hal
)
{
return
jIO
.
util
.
ajax
({
"
type
"
:
"
GET
"
,
"
url
"
:
UriTemplate
.
parse
(
site_hal
.
_links
.
raw_search
.
href
)
.
expand
({
query
:
options
.
query
,
// XXX Force erp5 to return embedded document
select_list
:
options
.
select_list
||
[
"
title
"
,
"
reference
"
],
limit
:
options
.
limit
}),
"
xhrFields
"
:
{
withCredentials
:
true
}
});
})
.
then
(
function
(
response
)
{
return
JSON
.
parse
(
response
.
target
.
responseText
);
})
.
then
(
function
(
catalog_json
)
{
var
data
=
catalog_json
.
_embedded
.
contents
,
count
=
data
.
length
,
i
,
uri
,
item
,
result
=
[],
promise_list
=
[
result
];
for
(
i
=
0
;
i
<
count
;
i
+=
1
)
{
item
=
data
[
i
];
uri
=
new
URI
(
item
.
_links
.
self
.
href
);
result
.
push
({
id
:
uri
.
segment
(
2
),
key
:
uri
.
segment
(
2
),
doc
:
{},
value
:
item
});
// if (options.include_docs) {
// promise_list.push(RSVP.Queue().push(function () {
// return this._get({_id: item.name}, {_view: "View"});
// }).push
// }
}
return
RSVP
.
all
(
promise_list
);
})
.
then
(
function
(
promise_list
)
{
var
result
=
promise_list
[
0
],
count
=
result
.
length
;
command
.
success
({
"
data
"
:
{
"
rows
"
:
result
,
"
total_rows
"
:
count
}});
})
.
fail
(
function
(
error
)
{
console
.
error
(
error
);
command
.
error
(
"
error
"
,
"
did not work as expected
"
,
"
Unable to call allDocs
"
);
});
};
jIO
.
addStorage
(
"
erp5
"
,
ERP5Storage
);
}(
jIO
,
complex_queries
,
URI
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment