Skip to content

Commit 3ceda4c

Browse files
author
Pat Patterson
committed
Remove RemoteTK, don't use proxy with REST API in Visualforce, add createBlob, updateBlob
1 parent 663af70 commit 3ceda4c

14 files changed

+299
-1314
lines changed

README.markdown

+49-46
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,51 @@ This minimal toolkit allows JavaScript in web pages to call the Force.com REST A
66
Background
77
----------
88

9-
Due to the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy), JavaScript running in Visualforce pages may not use [XmlHttpRequest](http://en.wikipedia.org/wiki/XMLHttpRequest) to directly invoke the REST API, since Visualforce pages have hostnames of the form abc.na1.visual.force.com, and the REST API endpoints are of the form na1.salesforce.com.
10-
11-
The RemoteTK Visualforce Custom Component (comprising RemoteTK.component and RemoteTKController.cls) provides an abstraction very similar to the REST API, implemented via `@RemoteAction` methods in the component's controller. The advantage of this mechanism is that no API calls are consumed.
12-
13-
Alternatively, the ForceTK JavaScript library works around the same origin restriction by using the [AJAX Proxy](http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_queryresultiterator.htm#ajax_proxy) to give full access to the REST API. Since the AJAX proxy is present on all
14-
Visualforce hosts with an endpoint of the form https://abc.na1.visual.force.com/services/proxy, our Visualforce-hosted JavaScript can invoke it, passing the desired resource URL in an HTTP header. A drawback here is that using the REST API, even from a Visualforce page, consumes API calls.
15-
16-
To host JavaScript *outside* the Force.com platform, we can deploy a simple PHP proxy to perform the same function as the AJAX proxy.
17-
18-
[PhoneGap](http://www.phonegap.com/) provides a way for HTML5/JavaScript apps to run as native applications; in this configuration a proxy is not required - the toolkit simply provides a convenient abstraction of the REST API.
9+
ForceTK provides a convenient, thin JavaScript abstraction of the [Force.com REST API](https://developer.salesforce.com/page/REST_API), making the API more accessible to JavaScript code running in Visualforce, in hybrid mobile apps, and elsewhere.
10+
11+
Due to the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy), JavaScript running outside the Force.com Platform may not use [XMLHttpRequest](http://en.wikipedia.org/wiki/XMLHttpRequest) to directly invoke the REST API, so a minimal PHP proxy is provided.
12+
13+
Recent Updates
14+
--------------
15+
16+
* [Visualforce Remote Objects](https://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_remote_objects.htm|StartTopic=Content%2Fpages_remote_objects.htm|SkinName=webhelp) are proxy objects that enable basic DML operations on sObjects directly from JavaScript. Behind the scenes, the Remote Objects controller handles sharing rules, field level security, and other data accessibility concerns. Pages that use Remote Objects are subject to all the standard Visualforce limits, but like JavaScript remoting, Remote Objects calls don’t count toward API request limits.
17+
18+
19+
Since Remote Objects are more secure than RemoteTK (which does not respect sharing rules, FLS etc since system-level access is proxied via the RemoteTK controller), and similarly do not consume API calls (the main motivation for RemoteTK), RemoteTK has been removed from the toolkit.
20+
21+
* Since the Summer '13 release, the `/services/data` endpoint has been exposed on Visualforce hosts, so no proxy is now required for REST API calls in JavaScript served via Visualforce (although the proxy **is** still required for calls to `/services/apexrest`). `forcetk.js` has been updated to reflect this.
22+
23+
* Inserting or updating blob data using the `create` or `update` functions (passing base64-encoded binary data in JSON) is limited by the REST API to 50 MB of text data or 37.5 MB of base64–encoded data. New functions, `createBlob` and `updateBlob`, allow creation and update of ContentVersion and Document records with binary ('blob') content with a size of up to 500 MB. Here is a minimal sample that shows how to upload a file to Chatter Files:
24+
25+
<apex:page>
26+
<script src="{!$Resource.forcetk_new}"></script>
27+
<p>
28+
Select a file to upload as a new Chatter File.
29+
</p>
30+
<input type="file" id="file" onchange="upload()"/>
31+
<p id="message"></p>
32+
<script>
33+
var client = new forcetk.Client();
34+
client.setSessionToken('{!$Api.Session_ID}');
35+
function upload() {
36+
var file = document.getElementById("file").files[0];
37+
client.createBlob('ContentVersion', {
38+
Origin: 'H', // 'H' for Chatter File, 'C' for Content Document
39+
PathOnClient: file.name
40+
}, file.name, 'VersionData', file, function(response){
41+
console.log(response);
42+
document.getElementById("message").innerHTML =
43+
"Chatter File created: <a href=\"/" + response.id + "\">Take a look!</a>";
44+
}, function(request, status, response){
45+
console.log(response);
46+
document.getElementById("message").innerHTML =
47+
"Error: " + status;
48+
});
49+
}
50+
</script>
51+
</apex:page>
52+
53+
Under the covers, `createBlob` sends a multipart message. See the REST API doc page [Insert or Update Blob Data](https://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm) for more details.
1954

2055
Dependencies
2156
------------
@@ -25,37 +60,8 @@ The toolkit uses [jQuery](http://jquery.com/). It has been tested on jQuery 1.4.
2560
Configuration
2661
-------------
2762

28-
RemoteTK requires no configuration.
29-
3063
ForceTK requires that you add the correct REST endpoint hostname for your instance (i.e. https://na1.salesforce.com/ or similar) as a remote site in *Your Name > Administration Setup > Security Controls > Remote Site Settings*.
3164

32-
Using RemoteTK in a Visualforce page
33-
------------------------------------
34-
35-
Add RemoteTKController.cls and RemoteTK.component to your org by creating an Apex Class and a Component and pasting in the respective content, pasting the files into a [Force.com IDE](http://wiki.developerforce.com/page/Force.com_IDE) project, or by using the [Force.com Migration Tool](http://wiki.developerforce.com/page/Migration_Tool_Guide).
36-
37-
Your Visualforce page will need to include the component and create a client object. An absolutely minimal sample is:
38-
39-
<apex:page>
40-
<!-- Include the RemoteTK component -->
41-
<c:RemoteTK />
42-
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" />
43-
<script type="text/javascript">
44-
// Get a reference to jQuery that we can work with
45-
$j = jQuery.noConflict();
46-
47-
// Get an instance of the RemoteTK client
48-
var client = new remotetk.Client();
49-
50-
client.query("SELECT Name FROM Account LIMIT 1", function(response){
51-
$j('#accountname').html(response.records[0].Name);
52-
});
53-
</script>
54-
<p>The first account I see is <span id="accountname"></span>.</p>
55-
</apex:page>
56-
57-
More fully featured samples are provided in [RemoteTKExample.page](Force.com-JavaScript-REST-Toolkit/blob/master/RemoteTKExample.page) and [RemoteTKMobile.page](Force.com-JavaScript-REST-Toolkit/blob/master/RemoteTKMobile.page).
58-
5965
Using ForceTK in a Visualforce page
6066
-----------------------------------
6167

@@ -67,15 +73,12 @@ Your Visualforce page will need to include jQuery and the toolkit, then create a
6773
<apex:includeScript value="{!URLFOR($Resource.static, 'jquery.js')}" />
6874
<apex:includeScript value="{!URLFOR($Resource.static, 'forcetk.js')}" />
6975
<script type="text/javascript">
70-
// Get a reference to jQuery that we can work with
71-
$j = jQuery.noConflict();
72-
7376
// Get an instance of the REST API client and set the session ID
7477
var client = new forcetk.Client();
7578
client.setSessionToken('{!$Api.Session_ID}');
7679
7780
client.query("SELECT Name FROM Account LIMIT 1", function(response){
78-
$j('#accountname').html(response.records[0].Name);
81+
$('#accountname').text(response.records[0].Name);
7982
});
8083
</script>
8184
<p>The first account I see is <span id="accountname"></span>.</p>
@@ -130,14 +133,14 @@ Your HTML page will need to include jQuery and the toolkit, then create a client
130133
function sessionCallback(oauthResponse) {
131134
if (typeof oauthResponse === 'undefined'
132135
|| typeof oauthResponse['access_token'] === 'undefined') {
133-
$('#message').html('Error - unauthorized!');
136+
$('#message').text('Error - unauthorized!');
134137
} else {
135138
client.setSessionToken(oauthResponse.access_token, null,
136139
oauthResponse.instance_url);
137140

138141
client.query("SELECT Name FROM Account LIMIT 1",
139142
function(response){
140-
$('#message').html('The first account I see is '
143+
$('#message').text('The first account I see is '
141144
+response.records[0].Name);
142145
});
143146
}
@@ -215,7 +218,7 @@ An absolutely minimal sample using OAuth to obtain a session ID is:
215218
216219
client.query("SELECT Name FROM Account LIMIT 1",
217220
function(response){
218-
$('#message').html('The first account I see is '
221+
$('#message').text('The first account I see is '
219222
+response.records[0].Name);
220223
}
221224
);

RemoteTK.component

-201
This file was deleted.

0 commit comments

Comments
 (0)