Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/pebble-my-data.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbahbka <bahbka@gmail.com>2014-07-16 01:07:50 +0400
committerbahbka <bahbka@gmail.com>2014-07-16 01:07:50 +0400
commit44a783b77111ab0b2a4ad6c019ed608cf81f4a37 (patch)
tree23caa9dd0a9d9478831a6c94cfe702f8bf7f0707
parent5c069c4e2b8e05aaeb55b93ce783f47fd3cda3ef (diff)
Extract fields from any level of JSON.
-rw-r--r--src/js/pebble-js-app.src.js47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/js/pebble-js-app.src.js b/src/js/pebble-js-app.src.js
index 30a03f8..0be03ec 100644
--- a/src/js/pebble-js-app.src.js
+++ b/src/js/pebble-js-app.src.js
@@ -25,6 +25,50 @@ var config = {
"config_shake": false
};
+function extract_fields(raw) {
+ var STR_FIELD = 1;
+ var INT_FIELD = 2;
+
+ var FIELDS = {
+ "content": STR_FIELD,
+ "refresh": INT_FIELD,
+ "vibrate": INT_FIELD,
+ "font": INT_FIELD,
+ "theme": INT_FIELD,
+ "scroll": INT_FIELD,
+ "light": INT_FIELD,
+ "blink": INT_FIELD,
+ "updown": INT_FIELD,
+ "auth": STR_FIELD
+ };
+
+ var result = {};
+
+ function parse(obj) {
+ for (var field in obj) {
+ if (typeof obj[field] === 'object'){
+ parse(obj[field]);
+ } else if (field in FIELDS) {
+ if (FIELDS[field] == STR_FIELD) {
+ result[field] = (result[field] === undefined ? '' : result[field] + '\n\n') + obj[field];
+ } else if (FIELDS[field] == INT_FIELD && result[field] === undefined) {
+ if (typeof obj[field] === 'string') {
+ var i = parseInt(obj[field]);
+ if (!isNaN(i)) {
+ result[field] = i;
+ }
+ } else {
+ result[field] = obj[field];
+ }
+ }
+ }
+ }
+ }
+
+ parse(raw);
+ return result;
+}
+
function http_request(url) {
var req = new XMLHttpRequest();
@@ -45,8 +89,9 @@ function http_request(url) {
if (req.readyState == 4) {
if(req.status == 200) {
try {
- var response = JSON.parse(req.responseText);
+ var response = extract_fields(JSON.parse(req.responseText));
//console.log("success: " + JSON.stringify(response));
+
response["msg_type"] = MSG.JSON_RESPONSE;
if (response["content"] && response["content"].length > CONTENT_MAX_LENGTH) {