This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add client identifier * Update demo * Separate provider class * Version bump
- Loading branch information
Showing
6 changed files
with
95 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,13 @@ | ||
<html> | ||
|
||
<head> | ||
<script | ||
src="https://code.jquery.com/jquery-3.2.1.min.js" | ||
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | ||
crossorigin="anonymous"></script> | ||
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | ||
crossorigin="anonymous"></script> | ||
<!-- TODO improve example to use local distribution build !--> | ||
<script src="../dist/fh-sync.js" type="text/javascript"></script> | ||
<script src="./sync.js" type="text/javascript"></script> | ||
</head> | ||
<body> | ||
|
||
<script type="text/javascript"> | ||
// Standard sync setup for simple dataset | ||
var datasetId = "myShoppingList"; | ||
|
||
//provide sync init options | ||
$fh.sync.init({ | ||
"cloudUrl": "http://localhost:3000", | ||
"sync_frequency": 10, | ||
"do_console_log": true, | ||
"storage_strategy": "dom" | ||
}); | ||
|
||
//provide listeners for notifications. | ||
$fh.sync.notify(function(notification){ | ||
var code = notification.code | ||
console.log("Sync ", notification); | ||
if('sync_complete' === code){ | ||
//a sync loop completed successfully, list the update data | ||
$fh.sync.doList(datasetId, | ||
function (res) { | ||
console.log('Successful result from list:', JSON.stringify(res)); | ||
}, | ||
function (err) { | ||
console.log('Error result from list:', JSON.stringify(err)); | ||
}); | ||
} else { | ||
//choose other notifications the app is interested in and provide callbacks | ||
} | ||
}); | ||
|
||
//manage the data set, repeat this if the app needs to manage multiple datasets | ||
var query_params = {}; //or something like this: {"eq": {"field1": "value"}} | ||
var meta_data = {}; | ||
$fh.sync.manage(datasetId, {}, query_params, meta_data, function(){ | ||
|
||
}); | ||
</script> | ||
Messages will be printed to console. | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Standard sync setup for simple dataset | ||
var datasetId = "myShoppingList"; | ||
|
||
//provide sync init options | ||
$fh.sync.init({ | ||
"cloudUrl": "http://localhost:3000", | ||
"sync_frequency": 10, | ||
"do_console_log": true, | ||
"storage_strategy": "dom" | ||
}); | ||
|
||
//provide listeners for notifications. | ||
$fh.sync.notify(function (notification) { | ||
var code = notification.code | ||
if ('sync_complete' === code) { | ||
//a sync loop completed successfully, list the update data | ||
$fh.sync.doList(datasetId, | ||
function (res) { | ||
console.log('Successful result from list:', JSON.stringify(res)); | ||
}, | ||
function (err) { | ||
console.log('Error result from list:', JSON.stringify(err)); | ||
}); | ||
} else { | ||
//choose other notifications the app is interested in and provide callbacks | ||
} | ||
}); | ||
|
||
//manage the data set, repeat this if the app needs to manage multiple datasets | ||
var query_params = {}; //or something like this: {"eq": {"field1": "value"}} | ||
var meta_data = {}; | ||
$fh.sync.manage(datasetId, {}, query_params, meta_data, function () { | ||
// Save data | ||
var data = { name: "Shoppping item1" }; | ||
$fh.sync.doCreate(datasetId, data, | ||
function (res) { | ||
console.log('Successful result from list:', JSON.stringify(res)); | ||
}, | ||
function (err) { | ||
console.log('Error result from list:', JSON.stringify(err)); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var uuidGenerator = require('uuid').v1; | ||
var CLIENT_ID_TAG = "feedhenry_sync_client"; | ||
|
||
/** | ||
* Get unique client id for current browser/platform/user | ||
*/ | ||
function getClientId() { | ||
if (window && window.device) { | ||
return window.device.uuid; | ||
} | ||
if (navigator && navigator.device) { | ||
return navigator.device.uuid; | ||
} | ||
if (window && window.localStorage) { | ||
var clientId = window.localStorage.getItem(CLIENT_ID_TAG); | ||
if (!clientId) { | ||
clientId = uuidGenerator(); | ||
localStorage.setItem(CLIENT_ID_TAG, clientId); | ||
} | ||
return clientId; | ||
} else { | ||
throw Error("Cannot create and store client id"); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getClientId: getClientId | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters