Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Roque
jio
Commits
cc8c320f
Commit
cc8c320f
authored
Nov 23, 2012
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updating localstorage put method
parent
0536cec8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
23 deletions
+76
-23
src/jio.storage/localstorage.js
src/jio.storage/localstorage.js
+76
-23
No files found.
src/jio.storage/localstorage.js
View file @
cc8c320f
...
...
@@ -210,6 +210,30 @@ var newLocalStorage = function ( spec, my ) {
return
obj
;
};
/**
* Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting
* with an underscore.
* ex: doc: {key:value1,_key:value2} with
* new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}.
* @param {object} doc The original document object.
* @param {object} new_doc The new document object
*/
priv
.
documentObjectUpdate
=
function
(
doc
,
new_doc
)
{
var
k
;
for
(
k
in
doc
)
{
if
(
k
[
0
]
!==
'
_
'
)
{
delete
doc
[
k
];
}
}
for
(
k
in
new_doc
)
{
if
(
k
[
0
]
!==
'
_
'
)
{
doc
[
k
]
=
new_doc
[
k
];
}
}
};
/**
* Create a document in the local storage.
* It will store the file in 'jio/local/USR/APP/FILE_NAME'.
...
...
@@ -285,37 +309,66 @@ var newLocalStorage = function ( spec, my ) {
* @method put
*/
that
.
put
=
function
(
command
)
{
var
now
=
Date
.
now
();
// wait a little in order to simulate asynchronous saving
setTimeout
(
function
()
{
var
secured_docid
=
priv
.
secureDocId
(
command
.
getDocId
()),
doc
=
null
,
path
=
'
jio/local/
'
+
priv
.
secured_username
+
'
/
'
+
priv
.
secured_application
name
+
'
/
'
+
secured_
docid
;
var
docid
,
doc
,
docpath
,
attmtid
,
attmt
,
attmtpath
,
prev_rev
;
doc
id
=
command
.
getDocId
();
prev_rev
=
command
.
getDocInfo
(
'
_rev
'
);
docpath
=
'
jio/local/
'
+
priv
.
secured_user
name
+
'
/
'
+
priv
.
secured_applicationname
+
'
/
'
+
docid
;
if
(
!
priv
.
checkSecuredDocId
(
secured_docid
,
command
.
getDocId
(),
'
put
'
))
{
return
;}
// reading
doc
=
LocalOrCookieStorage
.
getItem
(
path
);
doc
=
localstorage
.
getItem
(
doc
path
);
if
(
!
doc
)
{
// create document
doc
=
{
_id
:
command
.
getDocId
(),
content
:
command
.
getDocContent
(),
_creation_date
:
Date
.
now
(),
_last_modified
:
Date
.
now
()
};
if
(
!
priv
.
userExists
(
priv
.
secured_username
))
{
priv
.
addUser
(
priv
.
secured_username
);
that
.
error
({
status
:
404
,
statusText
:
'
Not found
'
,
error
:
'
not_found
'
,
message
:
'
Document not found.
'
,
reason
:
'
document not found
'
});
return
;
}
if
(
doc
.
_rev
!==
prev_rev
)
{
// want to update an older document
that
.
error
({
status
:
409
,
statusText
:
'
Conflict
'
,
error
:
'
conflict
'
,
message
:
'
Document update conflict.
'
,
reason
:
'
document update conflict.
'
});
return
;
}
priv
.
addFileName
(
secured_docid
);
// it is the good document
attmtid
=
command
.
getAttachmentId
();
if
(
attmtid
)
{
attmtpath
=
docpath
+
'
/
'
+
attmtid
;
// this is an attachment
attmt
=
localstorage
.
getItem
(
attmtpath
);
if
(
!
attmt
)
{
// there is no attachment to update
that
.
error
({
status
:
404
,
statusText
:
'
Not found
'
,
error
:
'
not_found
'
,
message
:
'
Document is missing attachment.
'
,
reason
:
'
document is missing attachment
'
});
return
;
}
// updating attachment
doc
.
_attachments
[
attmtid
].
revpos
=
parseInt
(
doc
.
_rev
.
split
(
'
-
'
)[
0
],
10
);
localstorage
.
setItem
(
attmtpath
,
command
.
getContent
());
}
else
{
// overwriting
doc
.
content
=
command
.
getDocContent
();
doc
.
_last_modified
=
Date
.
now
();
// update document metadata
priv
.
documentObjectUpdate
(
doc
,
command
.
cloneDoc
());
}
localStorage
.
setItem
(
path
,
doc
);
that
.
success
({
ok
:
true
,
id
:
command
.
getDocId
()});
doc
.
_rev
=
priv
.
generateNextRev
(
prev_rev
,
''
+
doc
+
'
'
+
now
+
''
);
localstorage
.
setItem
(
docpath
,
doc
);
that
.
success
(
priv
.
manageOptions
(
{
ok
:
true
,
id
:
docid
,
rev
:
doc
.
_rev
},
command
,
doc
)
);
});
};
// end put
...
...
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