Created
December 19, 2014 16:48
-
-
Save shimy/ed319fc37ccdd61a4af3 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Cognito Sync</title> | |
<script src="aws-sdk.min.js" type="text/javascript"></script> | |
<script src="dist/amazon-cognito.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<script> | |
var client = null; | |
AWS.config.region = 'us-east-1'; | |
var cognitoParams = { | |
AccountId: "xxxxxxxx", | |
RoleArn: "arn:aws:iam::xxxxxxxx:role/Cognito_CognitoSyncUnauth_DefaultRole", | |
IdentityPoolId: "us-east-1:xxxxxxx-xxxx-xxxxx-xxxxx-xxxxxxxx" | |
}; | |
AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams); | |
AWS.config.credentials.get(function() { | |
client = new AWS.CognitoSyncManager(); | |
client.openOrCreateDataset("myDatasetName", function(err, dataset) { | |
dataset.get("myData", function(err, value) { | |
console.log("myData = " + value); | |
}); | |
dataset.put('myData', 'newValue', function(err, record) { | |
if ( !err ) { | |
dataset.synchronize({ | |
onSuccess: function(dataset, newRecords) { | |
console.log("Data saved successfully."); | |
}, | |
onFailure: function(err) { | |
console.log("Error occured."); | |
}, | |
onConflict: function(dataset, conflicts, callback) { | |
var resolved = []; | |
for (var i=0; i < conflicts.length; i++) { | |
resolved.push(conflicts[i].resolveWithRemoteRecord()); | |
} | |
dataset.resolve(resolved, function(err) { | |
callback(true); | |
}); | |
}, | |
onDatasetDeleted: function(dataset, datasetName, callback) { | |
return callback(true); | |
}, | |
onDatasetMerged: function(dataset, datasetNames, callback) { | |
return callback(false); | |
} | |
}); | |
} | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment