Permalink
Browse files

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

…y.mobile part.
  • Loading branch information...
rsaccon committed Nov 20, 2010
1 parent f0a8ed4 commit 48c42543bd39feefb19558e7dca7ffee9baf4d93
@@ -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>Form submission</h1>
</form>
</div>
</div>
- <script type="text/javascript">
- $('#reset').click(function() {
- persistence.reset();persistence.schemaSync();
- return false;
- });
- </script>
</body>
</html>
View
@@ -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
@@ -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
@@ -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);
Oops, something went wrong.

0 comments on commit 48c4254

Please sign in to comment.