Permalink
Browse files

ajax wrapper added for wp7 support

  • Loading branch information...
1 parent 62d008c commit 32f55f653085b4781b58eaef6bdb6176d8b00cf5 @dipspb dipspb committed Feb 7, 2012
Showing with 76 additions and 28 deletions.
  1. +5 −4 app/layout.erb
  2. +71 −24 public/js/jquery-wp7-patch.js
View
@@ -18,8 +18,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<% end %>
- <% if System::get_property('platform') == 'APPLE' || System::get_property('platform') == 'ANDROID' || is_bb6 || System::get_property('webview_framework') =~ /^WEBKIT/ %>
- <script src="/public/jquery/jquery-1.6.4.min.js" type="text/javascript"></script>
+ <% if System::get_property('platform') == 'APPLE' || System::get_property('platform') == 'ANDROID' || System::get_property('platform') == 'WP7' || is_bb6 || System::get_property('webview_framework') =~ /^WEBKIT/ %>
+ <script src="/public/jquery/jquery-1.6.4.js" type="text/javascript"></script>
<link rel="stylesheet" href="/public/jqmobile/jquery.mobile-1.0.css">
<link rel="stylesheet" href="/public/css/jqmobile-patch.css">
@@ -59,10 +59,10 @@
//$.mobile.loadingMessageDelay = 50; // in ms
});
</script>
- <script type="text/javascript" charset="utf-8" src="/public/jqmobile/jquery.mobile-1.0.min.js"></script>
<% if System::get_property('platform') == 'WP7'%>
<script type="text/javascript" charset="utf-8" src="/public/js/jquery-wp7-patch.js"></script>
<% end %>
+ <script type="text/javascript" charset="utf-8" src="/public/jqmobile/jquery.mobile-1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="/public/js/jqmobile-patch.js"></script>
<% if $pagesize > 0 %>
<script type="text/javascript" charset="utf-8" src="/public/js/jquery.endless-scroll.js"></script>
@@ -79,7 +79,7 @@
<% elsif System::get_property('platform') == 'Blackberry' %>
<link href="/public/css/blackberry.css" type="text/css" rel="stylesheet"/>
<% elsif System::get_property('platform') == 'WP7' %>
- <link href="/public/css/windows_mobile.css" type="text/css" rel="stylesheet"/>
+ <link href="/public/css/iphone.css" type="text/css" rel="stylesheet"/>
<script src="/public/js/wp7.js" type="text/javascript"></script>
<% elsif System::get_property('webview_framework') =~ /^WEBKIT/ %>
<link href="/public/css/android.css" type="text/css" rel="stylesheet"/>
@@ -94,6 +94,7 @@
<% elsif System::get_property('platform') == 'WP7'%>
onload="wp7_change_forms_action_to_get()"
<% end %>
+ data-platform="<%= System::get_property('platform') %>"
>
<%= @content %>
</body>
@@ -1,5 +1,37 @@
(function($){
+ var _rho_deferred_paramName = "_rho_deferred";
+ var _rho_callbackId_paramName = "_rho_callbackId";
+ var _rho_callbackId_valuePrefix = "_rhoId#";
+
+ // console function proxies ============================================
+ if ("object" != typeof console) console = {};
+
+ if ("function" != typeof console.log) console.log = function(msg) {
+ window.external.notify('console.log: ' +msg);
+ };
+
+ if ("function" != typeof console.info) console.info = console.log;
+
+ if ("function" != typeof console.warn) console.warn = function(msg) {
+ window.external.notify('console.warn: ' +msg);
+ };
+
+ if ("function" != typeof console.error) console.error = function(msg) {
+ window.external.notify('console.error: ' +msg);
+ };
+
+ if ("function" != typeof window.alert && "object" != typeof window.alert) window.alert = function(msg) {
+ window.external.notify('alert: ' +msg);
+ };
+ // =====================================================================
+
+ // do nothing if jQuery is undefined
+ if (!$) {
+ console.error('jQuery is undefined');
+ return;
+ }
+
var callbackCount = 0;
var pendingCallbacks = {};
@@ -12,24 +44,30 @@
return {};
}
- var _ajax = jQuery.ajax;
+ var _ajax = $.ajax;
function wp7ajax() {
+ //console.log('in wp7ajax');
+ //console.log('window.location: ' +window.location);
// there may be ajax(options) or ajax(url, options) form of call
var options = normalizeAjaxOptions.apply(this, arguments);
+ //console.log('options has been normalized, options.url == "' +options.url +'"');
// if we have URL in request options and it is a local URL
- if (options.url.match(/^https?:\/\/(127\.0\.0\.1|localhost)/)) {
+ if (options.url.match(/^https?:\/\/(127\.0\.0\.1|localhost)/) ||
+ options.url.match(/^x-wmapp1:/)) {
+ //console.log('it is a local app URL');
// then call proxy
return wp7notifyProxy.apply(this, [options]);
}
// else, let default AJAX function to handle it
return _ajax.apply(this, arguments);
}
- jQuery.ajax = wp7ajax;
+ $.ajax = wp7ajax;
function wp7notifyProxy(options) {
+ //console.log('in wp7notifyProxy');
/*
* IMPORTANT NOTE! ===========================================
* At the moment this implementation doesn't support anything
@@ -38,30 +76,37 @@
* ===========================================================
*/
+ //console.log('options.url: ' + options.url);
+ //console.log('typeof options.data: ' + typeof options.data);
+ //if ("string" == typeof options.data) console.log('options.data: ' + options.data);
+
+ var urlQueryParams = "";
// set next call id value
- options.data = options.data || {};
- options.data._rho_callbackId = "_rhoId#" + callbackCount++;
+ var cbIdValue = encodeURIComponent(_rho_callbackId_valuePrefix + callbackCount++);
- // set deferred object to resolve/reject late
- options._rho_deferred = $.Deferred();
+ if ("string" == typeof options.data) {
+ urlQueryParams = options.data;
+ }
+ if ("object" != typeof options.data) {
+ options.data = {};
+ }
+ // set callback id param value
+ options.data[_rho_callbackId_paramName] = cbIdValue;
+ // set deferred object to resolve/reject late
+ options[_rho_deferred_paramName] = $.Deferred();
// store options for pending callback
- pendingCallbacks[options.data._rho_callbackId] = options;
+ pendingCallbacks[options.data[_rho_callbackId_paramName]] = options;
// compose GET request formatted URI
- var request = options.url;
- var isFirstParam = true;
$.each(options.data, function(name, value){
- request = request + ((isFirstParam ? '?' : '&')
+ urlQueryParams += ((0 < urlQueryParams.length ? "&" : "")
+encodeURIComponent(name) +'=' +encodeURIComponent(value));
- isFirstParam = false;
});
- //console.log('AJAX: this = ' +this +' arguments = ' +arguments);
- console.log('wrapped AJAX request URI:' + request);
- return _ajax.apply(this, arguments);
- //return window.external.notify(request);
-
+ //console.log('wp7notifyProxy: urlQueryParams: ' +urlQueryParams);
+ window.external.notify('request:' +options.url +"?" +urlQueryParams);
+ return options[_rho_deferred_paramName];
}
function fireHandlers(options, result, status, errCode) {
@@ -75,8 +120,8 @@
options.error.apply(this, [jqXHR, status, result]);
}, 1);
}
- if ('object' == typeof options._rho_deferred) {
- options._rho_deferred.resolve([jqXHR, status, result]);
+ if ('object' == typeof options[_rho_deferred_paramName]) {
+ options[_rho_deferred_paramName].resolve([jqXHR, status, result]);
}
} else {
if ('function' == typeof options.success) {
@@ -85,16 +130,18 @@
options.success.apply(this, [result, status, jqXHR]);
}, 1);
}
- if ('object' == typeof options._rho_deferred) {
- options._rho_deferred.resolve([result, status, jqXHR]);
+ if ('object' == typeof options[_rho_deferred_paramName]) {
+ options[_rho_deferred_paramName].resolve([result, status, jqXHR]);
}
}
}
window._rho_ajaxProxyCallback = function(callbackId, result, status, errCode) {
- if (pendingCallbacks[callbackId]) {
- fireHandlers(pendingCallbacks[callbackId], result, status, errCode);
- delete pendingCallbacks[callbackId];
+ var cbId = decodeURIComponent(callbackId);
+ //console.log('_rho_ajaxProxyCallback: callback for: ' +cbId);
+ if (pendingCallbacks[cbId]) {
+ fireHandlers(pendingCallbacks[cbId], result, status, errCode);
+ delete pendingCallbacks[cbId];
}
};

0 comments on commit 32f55f6

Please sign in to comment.