Permalink
Browse files

Turned the system connect service resource into JSON. Started trying …

…to figure out how to do local storage service resource result dependency clean up. This code is high-ly experimental.
  • Loading branch information...
1 parent 9e5e7c4 commit cc9cfcdae44d26a68451d8819843c0b9fc34e3d8 Tyler Frankenstein committed Feb 29, 2012
@@ -114,7 +114,7 @@ $('#drupalgap_page_comment_edit_submit').live('click',function(){
// Existing comment.
// retrieve the comment, update the values
- comment = drupalgap_services_comment_retrieve({"cid":drupalgap_page_comment_edit_cid,"from_local_storage":"0"});
+ comment = drupalgap_services_comment_retrieve({"cid":drupalgap_page_comment_edit_cid,"load_from_local_storage":"0"});
if (!comment) {
alert("drupalgap_page_comment_edit_submit - failed to load comment (" + drupalgap_page_comment_edit_cid + ")");
}
@@ -20,7 +20,7 @@ $('#drupalgap_page_node_edit').live('pageshow',function(){
$('#drupalgap_page_node_edit_delete').hide();
}
else { // existing node...
- node = drupalgap_services_node_retrieve({"nid":drupalgap_page_node_edit_nid,"from_local_storage":"0"});
+ node = drupalgap_services_node_retrieve({"nid":drupalgap_page_node_edit_nid,"load_from_local_storage":"0"});
content_type = drupalgap_services_content_type_load(node.type);
$('#drupalgap_page_node_edit h1').html("Edit " + content_type.name);
if (!node) {
@@ -68,7 +68,7 @@ $('#drupalgap_page_node_edit_submit').live('click',function(){
}
else { // existing nodes...
// retrieve the node, update the values
- node = drupalgap_services_node_retrieve({"nid":drupalgap_page_node_edit_nid,"from_local_storage":"0"});
+ node = drupalgap_services_node_retrieve({"nid":drupalgap_page_node_edit_nid,"load_from_local_storage":"0"});
if (!node) {
alert("drupalgap_page_node_edit_submit - failed to load node (" + drupalgap_page_node_edit_nid + ")");
}
@@ -1,7 +1,7 @@
<div data-role="page" id="drupalgap_page_setup">
<div data-role="header">
<a href="welcome.html" data-icon="back" class="ui-btn-left">Back</a>
- <h1>DrupalGap Install</h1>
+ <h1>DrupalGap Setup</h1>
<a href="http://www.drupalgap.org" data-icon="gear" class="ui-btn-right">Help</a>
</div><!-- /header -->
<div data-role="header" data-theme="c">
View
@@ -40,7 +40,8 @@ $('#drupalgap_page_setup_connect').live('click',function(){
drupalgap_settings_save(settings);
// perform system connect to see if drupalgap is setup properly on drupal site
- result = drupalgap_services_system_connect();
+ //result = drupalgap_services_system_connect();
+ result = drupalgap_services_system_connect.resource_call();
if (result.errorThrown || result.textStatus == "error") { // something went wrong...
// Clear the site path and re-save the settings to start over.
@@ -16,7 +16,8 @@ $('#drupalgap_page_welcome_button_demo').live('click',function(){
drupalgap_settings_save(settings);
// perform system connect to see if drupalgap is setup properly on drupal site
- result = drupalgap_services_system_connect();
+ //result = drupalgap_services_system_connect();
+ result = drupalgap_services_system_connect.resource_call();
if (result.errorThrown) { // something went wrong...
// clear the site path and re-save the settings to start over
@@ -66,7 +66,7 @@ function drupalgap_services_comment_create (comment) {
*
* options.cid
* the comment id you want to load
- * options.from_local_storage
+ * options.load_from_local_storage
* load comment from local storage
* "0" = force reload from comment retrieve resource
* "1" = grab from local storage if possible (default)
@@ -86,13 +86,13 @@ function drupalgap_services_comment_retrieve (options) {
// If everything is valid, retrieve the comment.
if (valid) {
- // If no from_local_storage option is set, set default.
- if (!options.from_local_storage) {
- options.from_local_storage = "1";
+ // If no load_from_local_storage option is set, set default.
+ if (!options.load_from_local_storage) {
+ options.load_from_local_storage = "1";
}
// Try to load the comment from local storage if loading from cache.
- if (options.from_local_storage == "1") {
+ if (options.load_from_local_storage == "1") {
comment = window.localStorage.getItem("comment." + options.cid);
}
View
@@ -39,7 +39,7 @@ function drupalgap_services_node_create (node) {
*
* options.nid
* the node id you want to load
- * options.from_local_storage
+ * options.load_from_local_storage
* load node from local storage
* "0" = force reload from node retrieve resource
* "1" = grab from local storage if possible (default)
@@ -58,13 +58,13 @@ function drupalgap_services_node_retrieve (options) {
if (valid) {
- // If no from_local_storage option is set, set default.
- if (!options.from_local_storage) {
- options.from_local_storage = "1";
+ // If no load_from_local_storage option is set, set default.
+ if (!options.load_from_local_storage) {
+ options.load_from_local_storage = "1";
}
// Try to load the node from local storage if loading from cache.
- if (options.from_local_storage == "1") {
+ if (options.load_from_local_storage == "1") {
node = window.localStorage.getItem("node." + options.nid);
}
@@ -17,7 +17,11 @@ var drupalgap_services_resource_call_result;
* The data type to use in the ajax call (default: json)
* options.data
* The data string to send with the ajax call (optional)
- * options.from_local_storage
+ * options.load_from_local_storage
+ * Load service resource call from local storage.
+ * "0" = force reload from service resource
+ * "1" = grab from local storage if possible (default)
+ * options.save_to_local_storage
* Load service resource call from local storage.
* "0" = force reload from service resource
* "1" = grab from local storage if possible (default)
@@ -61,21 +65,30 @@ function drupalgap_services_resource_call (options) {
if (!options.dataType) {
options.dataType = "json";
}
- if (!options.from_local_storage) {
- options.from_local_storage = "1";
- }
// Build URL to service resource.
- var service_resource_call_url = options.site_path + options.base_path + options.endpoint + "/" + options.resource_path;
+ var service_resource_call_url = drupalgap_services_resource_url(options);
// Set default local storage key if one wasn't provided.
if (!options.local_storage_key) {
- options.local_storage_key = options.type + "." + service_resource_call_url;
+ options.local_storage_key = drupalgap_services_default_local_storage_key(options.type,service_resource_call_url);
}
+ // If no load_from_local_storage option was set, set the default
+ // for best performance based on the service resource call.
+ if (!options.load_from_local_storage) {
+ options.load_from_local_storage = "1";
+ // Services -> System -> Connect
+ /*if (options.resource_path.indexOf("system/connect") != -1) {
+ // We'll assume all calls to system connect should not go
+ // into local storage.
+ alert("system connect, no local storage");
+ options.load_from_local_storage = "0";
+ }*/
+ }
// If we are attempting to load the service resource result call from
// localstorage, do it now.
- if (options.from_local_storage == "1") {
+ if (options.load_from_local_storage == "1") {
result = window.localStorage.getItem(options.local_storage_key);
}
@@ -109,10 +122,33 @@ function drupalgap_services_resource_call (options) {
console.log(JSON.stringify({"path":service_resource_call_url,"options":options}));
console.log(JSON.stringify(result));
- // If there wasn't an error from the service call, save the result to local storage.
+ // If there wasn't an error from the service call...
if (!result.errorThrown) {
+
+ // Save the result to local storage.
window.localStorage.setItem(options.local_storage_key, JSON.stringify(result));
console.log("saving service resource to local storage (" + options.local_storage_key +")");
+
+ // If this service resource call has local storage items dependent on
+ // result, then remove those items from local storage.
+ /* Stuff with dependents:
+ * user: create/update/delete/login/logout/registration
+ * node: create/update/delete
+ * comment: create/update/delete
+ */
+ switch (options.type.toLowerCase()) {
+ case "get":
+ break;
+ case "post":
+ if (options.resource_path.indexOf("user/login") != -1) {
+
+ }
+ break;
+ case "put":
+ break;
+ case "delete":
+ break;
+ }
}
}
@@ -127,4 +163,42 @@ function drupalgap_services_resource_call (options) {
drupalgap_services_resource_call_result = result;
return drupalgap_services_resource_call_result;
+}
+
+/*
+ * Returns a URL to the service resource based on the incoming options.
+ *
+ * options.resource_path
+ * The path to the resource (required)
+ * options.site_path
+ * The full site path (default: drupalgap_settings.site_path)
+ * options.base_path
+ * The drupal base path (default: drupalgap_settings.base_path)
+ * options.endpoint
+ * The endpoint name (default : drupalgap_settings.services_endpoint_default)
+*/
+function drupalgap_services_resource_url(options) {
+ // Set default values for options if none were provided.
+ if (!options.site_path) {
+ options.site_path = drupalgap_settings.site_path;
+ }
+ if (!options.base_path) {
+ options.base_path = drupalgap_settings.base_path;
+ }
+ if (!options.endpoint) {
+ options.endpoint = drupalgap_settings.services_endpoint_default;
+ }
+ return options.site_path + options.base_path + options.endpoint + "/" + options.resource_path;
+}
+
+/*
+ * Returns a string key for local storage of a service call result.
+ *
+ * type
+ * The method to use: get, post, put, delete
+ * url
+ * The full URL to the service resource.
+ */
+function drupalgap_services_default_local_storage_key(type,url) {
+ return type + "." + url;
}
@@ -1,54 +1,51 @@
// global variables used to hold the latest system resource call results
-var drupalgap_services_system_connect_result;
+//var drupalgap_services_system_connect_result;
var drupalgap_services_system_get_variable_result;
-/**
- * Makes a Service call to Drupal's System Connect resource.
- *
- * @return
- * A JSON object containing information about the drupal user who made the service call, and NULL if the service call failed.
- */
-function drupalgap_services_system_connect () {
- try {
- // make the service call
- drupalgap_services_system_connect_result = drupalgap_services_resource_call({"resource_path":"system/connect.json"});
+var drupalgap_services_system_connect = {
- if (drupalgap_services_system_connect_result.textStatus == "error") { // there was a problem connecting...
- if (drupalgap_services_system_connect_result.errorThrown) {
- alert(drupalgap_services_system_connect_result.errorThrown);
- }
- else {
- alert(drupalgap_services_system_connect_result.textStatus);
- }
- }
- else { // the connect was successful...
- // save a copy of the current user
- drupalgap_user = drupalgap_services_system_connect_result.user;
- // make sure authenticated user's account is active
- if (drupalgap_user.uid != 0 && drupalgap_user.status != 1) {
- // TODO - this alert doesn't work... the forced logout seems to work though...
- alert("The username " + drupalgap_user.name + " has not been activated or is blocked.");
- drupalgap_services_user_logout();
- }
- else {
- // load drupalgap system settings (WARNING: this is a nested service call, need to figure out a way to bundle this
- // into one call and setup some caching and expiration feature layer on top of the main service resource call function
- // that utilizes local storage!)
+ "resource_path":"system/connect.json",
+ "resource_type":"post",
+ "resource_result":"",
+
+ /**
+ * Makes a Service call to Drupal's System Connect resource.
+ *
+ * @return
+ * A JSON object containing information about the drupal user who made the service call, and NULL if the service call failed.
+ */
+ "resource_call":function() {
+
+ try {
+
+ this.resource_result = drupalgap_services_resource_call({"resource_path":this.resource_path});
- // This has been moved to a bundled service call in services/drupalgap.js
- // drupalgap_site_settings = drupalgap_services_system_site_settings();
+ if (this.resource_result.textStatus == "error") { // there was a problem connecting...
+ if (this.resource_result.errorThrown) {
+ alert(this.resource_result.errorThrown);
+ }
+ else {
+ alert(this.resource_result.textStatus);
+ }
+ }
+ else { // the connect was successful...
+ // save a copy of the current user
+ drupalgap_user = this.resource_result.user;
+ // make sure authenticated user's account is active
+ if (drupalgap_user.uid != 0 && drupalgap_user.status != 1) {
+ // TODO - this alert doesn't work... the forced logout seems to work though...
+ alert("The username " + drupalgap_user.name + " has not been activated or is blocked.");
+ drupalgap_services_user_logout();
+ }
+ }
}
- }
-
- // return the result
- return drupalgap_services_system_connect_result;
- }
- catch (error) {
- console.log("drupalgap_services_system_connect");
- console.log(error);
- }
- return drupalgap_services_system_connect_result;
-}
+ catch (error) {
+ console.log("drupalgap_services_system_connect.resource_call");
+ console.log(error);
+ }
+ return this.resource_result;
+ },
+};
/**
* Makes an synchronous call to Drupal's System Get Variable Resource.
@@ -5,21 +5,21 @@ var drupalgap_views_datasource_retrieve_result;
*
* options.path
* the path to the views json display
- * options.from_local_storage
+ * options.load_from_local_storage
* load node from local storage
* "0" = force reload from views datasource
* "1" = grab from local storage if possible (default)
*/
function drupalgap_views_datasource_retrieve (options) {
try {
- // If no from_local_storage option is set, set default.
- if (!options.from_local_storage) {
- options.from_local_storage = "1";
+ // If no load_from_local_storage option is set, set default.
+ if (!options.load_from_local_storage) {
+ options.load_from_local_storage = "1";
}
// Try to load the views json from local storage, if necessary.
- if (options.from_local_storage == "1") {
+ if (options.load_from_local_storage == "1") {
views_json = window.localStorage.getItem("views_datasource." + options.path);
}

0 comments on commit cc9cfcd

Please sign in to comment.