Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored the jquery code and documentation into a jquery and jquerl…

…y.mobile part.
  • Loading branch information...
commit 48c42543bd39feefb19558e7dca7ffee9baf4d93 1 parent f0a8ed4
@rsaccon rsaccon authored
View
11 demo/jquerymobile/index.html
@@ -25,7 +25,8 @@
<script src="../../lib/persistence.store.sql.js"></script>
<script src="../../lib/persistence.store.websql.js"></script>
<script src="../../lib/persistence.store.memory.js"></script>
- <script src="../../lib/jquery.persistence.js"></script>
+ <script src="../../lib/persistence.jquery.js"></script>
+ <script src="../../lib/persistence.jquery.mobile.js"></script>
<script type="text/javascript">
if (location.protocol == "file:") {
alert("Didn't you read the README ? You need to load this page from a server.");
@@ -53,7 +54,7 @@
$('#reset').click(function() {
persistence.reset();
- //persistence.schemaSync();
+ persistence.schemaSync();
return false;
});
</script>
@@ -98,11 +99,5 @@ <h1 id="jqm-logo"><img src="assets/jquery-logo.png" alt="jQuery Mobile Framework
</form>
</div>
</div>
- <script type="text/javascript">
- $('#reset').click(function() {
- persistence.reset();persistence.schemaSync();
- return false;
- });
- </script>
</body>
</html>
View
46 docs/jquery.md
@@ -1,6 +1,6 @@
-# jquery.persistence.js
+# persistence.jquery.js
-`jquery.persistence.js` is a jquery plugin for `persistence.js` that
+`persistence.jquery.js` is a jquery plugin for `persistence.js` that
allows the usage of jquery notation for crossbrowser-access of
persistencejs entities.
@@ -22,44 +22,4 @@ Simple example:
// getter
console.log($(user).data('firstname')); // => Mike
-You can find more examples in `test/test.persistence-jquery.js`.
-
-
-## jQuery mobile integration
-jQuery mobile (jqm) ajax request re-routing to persitencejs for:
-
-* html text: caches ajax-loaded HTML pages in local DB
-* images (in `img` tags of ajax-loaded HTML pages): grabs/encodes them via `canvas` and caches them as data-URL strings in local DB
-* form submission (only POST requests)
-
-For ajax-loaded HTML pages and images, the content-providing entities get
-their name from user-overwritable default values. For form submissions, the entity
-is matched according to the following URL pattern:
-
- entity-name / path1/path2/../pathN
-
-Ajax re-routing to persitencejs only takes place if the required entities exist.
-
-Global settings (and it's default values):
-
- persistence.jqmPageEntityName = "Page"; // Html page entity name
- persistence.jqmImageEntityName = "Image"; // Image entity name
- persistence.jqmPathField = "path"; // Entity path-field name
- persistence.jqmDataField`= "data"; // Entity data-field name
-
-Ajax page loading example:
-
- URL: "about/intro.html"
- => entity name: "Page"
- => entity path field: "about/intro.html"
- => entity data field: (the HTML content of the page)
- Images:
- => entity name: "Image"
- => entity path field: (src attribute value of the related IMG tag)
- => entity data field: (the imgae data as Base64 encoded dataURL)
-
-Ajax form submission examples:
-
- URL (POST): "order/response.html"
- => entity name: "Order"
- => entity fields (other than path): retrieved from POST data
+You can find more examples in `test/test.persistence-jquery.js`.
View
49 docs/jquery.mobile.md
@@ -0,0 +1,49 @@
+# persistence.jquery.mobile.js
+
+`persistence.jquery.mobile.js` is a plugin for `persistence.js` and jQuery mobile that
+allows ajax request re-routing to persitencejs for:
+
+* html text: caches ajax-loaded HTML pages in local DB
+* images (in `img` tags of ajax-loaded HTML pages): grabs/encodes them via `canvas` and caches them as data-URL strings in local DB
+* form submission (only POST requests)
+
+For ajax-loaded HTML pages and images, the content-providing entities get
+their name from user-overwritable default values. For form submissions, the entity
+is matched according to the following URL pattern:
+
+ entity-name / path1/path2/../pathN
+
+Ajax re-routing to persitencejs only takes place if the required entities exist.
+
+Global settings (and it's default values):
+
+ persistence.jquery.mobile.pageEntityName = "Page"; // Html page entity name
+ persistence.jquery.mobile.imageEntityName = "Image"; // Image entity name
+ persistence.jquery.mobile.pathField = "path"; // Entity path-field name
+ persistence.jquery.mobile.dataField = "data"; // Entity data-field name
+
+
+Optional Regular Expression to exclude URLs from re-routing to persistencejs:
+
+ persistence.jquery.mobile.urlExcludeRx
+
+Example: `persistence.jquery.mobile.urlExcludeRx = /^\/admin\//;`
+(all URL paths starting with "/admin/" are excluded)
+
+
+Ajax page loading example:
+
+ URL: "about/intro.html"
+ => entity name: "Page"
+ => entity path field: "about/intro.html"
+ => entity data field: (the HTML content of the page)
+ Images:
+ => entity name: "Image"
+ => entity path field: (src attribute value of the related IMG tag)
+ => entity data field: (the imgae data as Base64 encoded dataURL)
+
+Ajax form submission examples:
+
+ URL (POST): "order/response.html"
+ => entity name: "Order"
+ => entity fields (other than path): retrieved from POST data
View
101 lib/persistence.jquery.js
@@ -0,0 +1,101 @@
+/**
+ * Copyright (c) 2010 Roberto Saccon <rsaccon@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+if (!window.jQuery) {
+ throw new Error("jQuery should be loaded before persistence.jquery.js");
+}
+
+if (!window.persistence) {
+ throw new Error("persistence.js should be loaded before persistence.jquery.js");
+}
+
+persistence.jquery = {}
+
+/**
+ * crossbrowser implementation for entity-property
+ */
+persistence.defineProp = function(scope, field, setterCallback, getterCallback) {
+ scope[field] = function(value) {
+ if (value === undefined) {
+ return getterCallback();
+ } else {
+ setterCallback(value);
+ return scope;
+ }
+ };
+};
+
+/**
+ * crossbrowser implementation for entity-property setter
+ */
+persistence.set = function(scope, fieldName, value) {
+ if (persistence.isImmutable(fieldName)) throw new Error("immutable field: "+fieldName);
+ scope[fieldName](value);
+ return scope;
+};
+
+/**
+ * crossbrowser implementation for entity-property getter
+ */
+persistence.get = function(arg1, arg2) {
+ var val = (arguments.length == 1) ? arg1 : arg1[arg2];
+ return (typeof val === "function") ? val() : val;
+};
+
+
+(function($){
+ var originalDataMethod = $.fn.data;
+
+ $.fn.data = function(name, data) {
+ if (this[0] && this[0]._session && (this[0]._session === window.persistence)) {
+ if (data) {
+ this[0][name](data);
+ return this;
+ } else {
+ return this[0][name]();
+ }
+ } else {
+ return originalDataMethod.apply(this, arguments);
+ }
+ };
+
+ if (persistence.sync) {
+ persistence.sync.getJSON = function(url, success) {
+ $.getJSON(url, null, success);
+ };
+
+ persistence.sync.postJSON = function(url, data, success) {
+ $.ajax({
+ url: url,
+ type: 'POST',
+ data: data,
+ dataType: 'json',
+ success: function(response) {
+ success(JSON.parse(response));
+ }
+ });
+ };
+ }
+})(jQuery);
View
130 lib/jquery.persistence.js → lib/persistence.jquery.mobile.js
@@ -23,67 +23,20 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
-if (!window.jQuery) {
- throw new Error("jQuery should be loaded before persistence.jquery.js");
-}
+if (!window.persistence.jquery) {
+ throw new Error("persistence.jquery.js should be loaded before persistence.jquery.mobile.js");
+}
-if (!window.persistence) {
- throw new Error("persistence.js should be loaded before persistence.jquery.js");
-}
+persistence.jquery.mobile = {}
-/**
- * crossbrowser implementation for entity-property
- */
-persistence.defineProp = function(scope, field, setterCallback, getterCallback) {
- scope[field] = function(value) {
- if (value === undefined) {
- return getterCallback();
- } else {
- setterCallback(value);
- return scope;
- }
- };
-};
-
-/**
- * crossbrowser implementation for entity-property setter
- */
-persistence.set = function(scope, fieldName, value) {
- if (persistence.isImmutable(fieldName)) throw new Error("immutable field: "+fieldName);
- scope[fieldName](value);
- return scope;
-};
-
-/**
- * crossbrowser implementation for entity-property getter
- */
-persistence.get = function(arg1, arg2) {
- var val = (arguments.length == 1) ? arg1 : arg1[arg2];
- return (typeof val === "function") ? val() : val;
-};
-
-
-(function($){
- var originalDataMethod = $.fn.data;
-
- $.fn.data = function(name, data) {
- if (this[0] && this[0]._session && (this[0]._session === window.persistence)) {
- if (data) {
- this[0][name](data);
- return this;
- } else {
- return this[0][name]();
- }
- } else {
- return originalDataMethod.apply(this, arguments);
- }
- };
-
- if ($.mobile && window.openDatabase) {
- persistence.jqmPageEntityName = "Page";
- persistence.jqmImageEntityName = "Image";
- persistence.jqmPathField = "path";
- persistence.jqmDataField = "data";
+(function($){
+ var $pjqm = persistence.jquery.mobile;
+
+ if (window.openDatabase) {
+ $pjqm.pageEntityName = "Page";
+ $pjqm.imageEntityName = "Image";
+ $pjqm.pathField = "path";
+ $pjqm.dataField = "data";
var originalAjaxMethod = $.ajax;
@@ -164,7 +117,8 @@ persistence.get = function(arg1, arg2) {
}
$.ajax = function(settings) {
- var entities = {}, urlPathSegments = settings.url.split("/");
+ var parsedUrl = parseUri(settings.url);
+ var entities = {}, urlPathSegments = parsedUrl.path.split("/");
if ((settings.type == "post") && (urlPathSegments.length > 1)) {
var entityName = (urlPathSegments[1].charAt(0).toUpperCase() + urlPathSegments[1].substring(1));
if (persistence.isDefined(entityName)) {
@@ -198,26 +152,28 @@ persistence.get = function(arg1, arg2) {
} else {
originalAjaxMethod(settings);
}
+ } else if (persistence.urlExcludeRx && persistence.urlExcludeRx.test(parsedUrl.path)) {
+ originalAjaxMethod(settings);
} else {
- if (persistence.isDefined(persistence.jqmPageEntityName)) {
- var Page = persistence.define(persistence.jqmPageEntityName);
- Page.findBy(persistence.jqmPathField, settings.url, function(page) {
+ if (persistence.isDefined($pjqm.pageEntityName)) {
+ var Page = persistence.define($pjqm.pageEntityName);
+ Page.findBy($pjqm.pathField, settings.url, function(page) {
if (page) {
//
// load page and images from persistencejs
//
if (settings.success) {
var pos = 0, countOuter = 0, countInner = 0;
- var inStr = page[persistence.jqmDataField](), outStr = "";
+ var inStr = page[$pjqm.dataField](), outStr = "";
var regExp = /(<img[^>]+src\s*=\s*[\'\"])([^\'\"]+)([\'\"][^>]*>)/ig;
var replaced = inStr.replace(regExp, function($0, $1, $2, $3, offset) {
countOuter++;
- if (persistence.isDefined(persistence.jqmImageEntityName)) {
- var Img = persistence.define(persistence.jqmImageEntityName);
- Img.findBy(persistence.jqmPathField, expand(settings.url, $2), function(image){
+ if (persistence.isDefined($pjqm.imageEntityName)) {
+ var Img = persistence.define($pjqm.imageEntityName);
+ Img.findBy($pjqm.pathField, expand(settings.url, $2), function(image){
countInner++;
if (image) {
- var imgTagStr = $1 + image[persistence.jqmDataField]() + $3;
+ var imgTagStr = $1 + image[$pjqm.dataField]() + $3;
outStr += inStr.substring(pos, offset) + imgTagStr;
pos = offset + imgTagStr.length;
} else {
@@ -236,7 +192,7 @@ persistence.get = function(arg1, arg2) {
});
if (replaced == inStr) {
settings.success(inStr);
- } else if (!persistence.isDefined(persistence.jqmImageEntityName)) {
+ } else if (!persistence.isDefined($pjqm.imageEntityName)) {
settings.success(outStr);
};
}
@@ -248,18 +204,18 @@ persistence.get = function(arg1, arg2) {
url: settings.url,
success: function(data) {
settings.success(data);
- if (persistence.isDefined(persistence.jqmPageEntityName)) {
+ if (persistence.isDefined($pjqm.pageEntityName)) {
var entities = [], crawlImages = false;
- var Page = persistence.define(persistence.jqmPageEntityName);
- if (persistence.isDefined(persistence.jqmImageEntityName)) {
- var Img = persistence.define(persistence.jqmImageEntityName), count = 0;
+ var Page = persistence.define($pjqm.pageEntityName);
+ if (persistence.isDefined($pjqm.imageEntityName)) {
+ var Img = persistence.define($pjqm.imageEntityName), count = 0;
$("#"+settings.url.replace(/\//g,"\\/").replace(/\./g,"\\.")+" img").each(function(i, img){
crawlImages = true;
count++;
$(img).load(function() {
var obj = {}, parsedImgSrc = parseUri(img.src);
- obj[persistence.jqmPathField] = parsedImgSrc.path;
- obj[persistence.jqmDataField] = base64Image(img, getImageType(parsedImgSrc));
+ obj[$pjqm.pathField] = parsedImgSrc.path;
+ obj[$pjqm.dataField] = base64Image(img, getImageType(parsedImgSrc));
entities.push(new Img(obj));
if (crawlImages && (--count == 0)) {
@@ -276,8 +232,8 @@ persistence.get = function(arg1, arg2) {
}
var obj = {};
- obj[persistence.jqmPathField] = settings.url;
- obj[persistence.jqmDataField] = data;
+ obj[$pjqm.pathField] = settings.url;
+ obj[$pjqm.dataField] = data;
entities.push(new Page(obj));
@@ -297,22 +253,4 @@ persistence.get = function(arg1, arg2) {
}
};
}
-
- if (persistence.sync) {
- persistence.sync.getJSON = function(url, success) {
- $.getJSON(url, null, success);
- };
-
- persistence.sync.postJSON = function(url, data, success) {
- $.ajax({
- url: url,
- type: 'POST',
- data: data,
- dataType: 'json',
- success: function(response) {
- success(JSON.parse(response));
- }
- });
- };
- }
-})(jQuery);
+})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.