2015-02-10 84 views
0

我已經通過forceios創建了混合salesforce應用程序,並且我可以顯示所有聯繫人。無法通過salesforce混合應用程序調用REST API(forceios)

但現在我想展示,所以我用REST API的報告,但該方法

forcetk.Client.prototype.ajax

是沒有得到所謂的,也沒有在Xcode顯示錯誤或瀏覽器。

我有共同的app.js與你的文件,請幫我...

app.js

VAR forceClient;

(函數(){

"use strict"; 

/* Adding platform (ios/android) specific css */ 
var platformStyle = document.createElement('link'); 
platformStyle.setAttribute('rel', 'stylesheet'); 
if (/Android/.test(navigator.userAgent)) { 
    platformStyle.setAttribute('href', 'css/ratchet-theme-android.css'); 
} else if (/iPhone/.test(navigator.userAgent)) { 
    platformStyle.setAttribute('href', 'css/ratchet-theme-ios.css'); 
} 
document.querySelector('head').appendChild(platformStyle); 


/* Wait until cordova is ready to initiate the use of cordova plugins and app launch */ 
document.addEventListener("deviceready", function() { 
    authenticateUser(showUsersList); 
}, false); 

/* Method to authenticate user with Salesforce Mobile SDK's OAuth Plugin */ 
var authenticateUser = function(successHandler, errorHandler) { 

    // Get salesforce mobile sdk OAuth plugin 
    var oauthPlugin = cordova.require("com.salesforce.plugin.oauth"); 

    // Call getAuthCredentials to get the initial session credentials 
    oauthPlugin.getAuthCredentials(
     // Callback method when authentication succeeds. 
     function (creds) { 
      // Create forcetk client instance for rest API calls 
      forceClient = new forcetk.Client(creds.clientId, creds.loginUrl); 
      forceClient.setSessionToken(creds.accessToken, "v31.0", creds.instanceUrl); 
      forceClient.setRefreshToken(creds.refreshToken); 

      // Call success handler and handover the forcetkClient 
      successHandler(forceClient); 


            var path = '/v29.0/analytics/reports/00OD0000001ZbP7MAK/instances'; 

            var method = 'POST'; 

            var error = null; 

            var payload = null; 

            var retry = null; 

            alert("1"); 

            forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) { 

            alert("2"); 

            var that = this; 
            var url = this.instanceUrl + '/services/data' + path; 

            return $j.ajax({ 
                type: method || "GET", 
                async: this.asyncAjax, 
                url: (this.proxyUrl !== null) ? this.proxyUrl: url, 
                contentType: method == "DELETE" ? null : 'application/json', 
                cache: false, 
                processData: false, 
                data: payload, 
                success: callback, 
                error: (!this.refreshToken || retry) ? error : function(jqXHR, textStatus, errorThrown) { 
                if (jqXHR.status === 401) { 
                that.refreshAccessToken(function(oauthResponse) { 
                      that.setSessionToken(oauthResponse.access_token, null, 
                           oauthResponse.instance_url); 
                      that.ajax(path, callback, error, method, payload, true); 
                      }, 
                      error); 
                } else { 
                error(jqXHR, textStatus, errorThrown); 
                } 
                }, 
                dataType: "json", 
                beforeSend: function(xhr) { 
                if (that.proxyUrl !== null) { 
                xhr.setRequestHeader('SalesforceProxy-Endpoint', url); 
                } 
                xhr.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId); 
                xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); 
                } 
                }); 
            } 



     }, 
     function (error) { 
      alert('Failed to authenticate user: ' + error); 
     } 
    ); 
} 

/* This method will render a list of users from current salesforce org */ 
var showUsersList = function(forceClient) { 

    fetchRecords(forceClient, function(data) { 
     var users = data.records; 
     var name; 

     var listItemsHtml = ''; 
     for (var i=0; i < users.length; i++) { 

      name = users[i].Name; 

      name = name.replace("'", "\\'"); 

      listItemsHtml += ('<li onClick="getDetails('+"'"+name+"'"+')"><a href="#page2" data-transition="slide" class="ui-btn ui-btn-icon-right ui-icon-carat-r ui-nodisc-icon">' + users[i].Name+ '</a></li>'); 
     } 

     document.querySelector('#users').innerHTML = listItemsHtml; 
    }) 
} 

/* This method will fetch a list of user records from salesforce. 
Just change the soql query to fetch another sobject. */ 
var fetchRecords = function (forceClient, successHandler) { 
    var soql = 'SELECT Id, Name FROM Contact'; 
    forceClient.query(soql, successHandler, function(error) { 
     alert('Failed to fetch users: ' + error); 
    }); 
}; 

})();

回調函數(){

alert("hello"); 

}

功能getDetails(名稱){

$("#name").html(""); 
$("#account").html(""); 
$("#title").html(""); 
$("#email").html(""); 
$("#phone").html(""); 
$("#fax").html(""); 
$("#mailingstreet").html(""); 
$("#mailingcity").html(""); 
$("#mailingcountry").html(""); 


name = name.replace("'", "\\'"); 

var query = "select name, account.name, title, email, phone, fax, mailingstreet, mailingcity, mailingcountry from contact where name='"+name+"'"; 


forceClient.query(query, function(response){ 

        alert(JSON.stringify(response.records[0])); 

        $("#name").html("Name :" + response.records[0].Name); 
        $("#account").html("Account :" +response.records[0].Account.Name); 
        $("#title").html("Title :" + response.records[0].Title); 
        $("#email").html("Email :" +response.records[0].Email); 
        $("#phone").html("Phone :" + response.records[0].Phone); 
        $("#fax").html("Fax :" +response.records[0].Fax); 
        $("#mailingstreet").html("Mailing Street :" +response.records[0].MailingStreet); 
        $("#mailingcity").html("Mailing City :" + response.records[0].MailingCity); 
        $("#mailingcountry").html("Mailing Country :" +response.records[0].MailingCountry); 



      }); 

}

回答

相關問題