@@ -53,143 +53,19 @@ an HTML5 mobile app using jQuery Mobile
5353<apex:includeScript value="{!URLFOR($Resource.static, '/mobile/jquery.mobile-1.0a4.min.js')}" />
5454-->
5555<apex:includeScript
56- value="{!URLFOR($Resource.forcetk, 'forcetk.js')}" />
56+ value="{!URLFOR($Resource.sample, 'forcetk.js')}" />
57+ <apex:includeScript
58+ value="{!URLFOR($Resource.sample, 'mobileapp.js')}" />
5759<script type="application/javascript">
5860// Get a reference to jQuery that we can work with
5961$j = jQuery.noConflict();
6062
6163// Get an instance of the REST API client
6264var client = new forcetk.Client('{!$Api.Session_ID}');
6365
64- // Populate the account list and set up click handling
65- function getAccounts(callback){
66- $j('#accountlist').empty();
67- client.query("SELECT Id, Name FROM Account ORDER BY Name LIMIT 20"
68- , function(response){
69- $j.each(response.records, function() {
70- var id = this.Id;
71- $j('<li></li>')
72- .hide()
73- .append('<a href="#"><h2>'+this.Name+'</h2></a>')
74- .click(function(e){
75- e.preventDefault();
76- $j.mobile.pageLoading();
77- // We could do this more efficiently by adding Industry and
78- // TickerSymbol to the fields in the SELECT, but we want to
79- // show dynamic use of the retrieve function...
80- client.retrieve("Account", id, "Name,Id,Industry,TickerSymbol"
81- , function(response){
82- $j('#Name').html(response.Name);
83- $j('#Industry').html(response.Industry);
84- $j('#TickerSymbol').html(response.TickerSymbol);
85- $j('#Id').val(response.Id);
86- $j.mobile.pageLoading(true);
87- $j.mobile.changePage('#detailpage',"slide",false,true);
88- });
89- })
90- .appendTo('#accountlist')
91- .show();
92- });
93-
94- $j('#accountlist').listview('refresh');
95-
96- if (typeof callback != 'undefined' && callback != null) {
97- callback();
98- }
99- });
100- }
101-
102- // Gather fields from the account form and create a record
103- function createHandler(e){
104- e.preventDefault();
105- var accountform = $j('#accountform');
106- var fields = {};
107- accountform.find('input').each(function() {
108- var child = $j(this);
109- if ( child.val().length > 0 && child.attr("name") != 'Id') {
110- fields[child.attr("name")] = child.val();
111- }
112- });
113- $j.mobile.pageLoading();
114- client.create('Account', fields, function(response) {
115- getAccounts(function(){
116- $j.mobile.pageLoading(true);
117- $j.mobile.changePage('#mainpage',"slide",true,true);
118- });
119- });
120- }
121-
122- // Gather fields from the account form and update a record
123- function updateHandler(e){
124- e.preventDefault();
125- var accountform = $j('#accountform');
126- var fields = {};
127- accountform.find('input').each(function() {
128- var child = $j(this);
129- if ( child.val().length > 0 && child.attr("name") != 'Id') {
130- fields[child.attr("name")] = child.val();
131- }
132- });
133- $j.mobile.pageLoading();
134- client.update('Account', accountform.find('#Id').val(), fields
135- , function(response) {
136- getAccounts(function(){
137- $j.mobile.pageLoading(true);
138- $j.mobile.changePage('#mainpage',"slide",true,true);
139- });
140- });
141- }
142-
143- // Ugh - this is required to change text on a jQuery Mobile button
144- // due to the way it futzes with things at runtime
145- function setButtonText(id, str) {
146- $j(id).html(str).parent().find('.ui-btn-text').text(str);
147- }
148-
14966// Kick things off...
15067$j(document).ready(function(){
151- // Add click listeners
152- $j('#newbtn').click(function(e){
153- // Show the 'New Account' form
154- e.preventDefault();
155- $j('#accountform')[0].reset();
156- $j('#accformheader').html('New Account');
157- setButtonText('#actionbtn','Create');
158- $j('#actionbtn').unbind('click.btn').bind('click.btn',createHandler);
159- $j.mobile.changePage('#editpage',"slide",false,true);
160- });
161-
162- $j('#deletebtn').click(function(e){
163- // Delete the account
164- e.preventDefault();
165- $j.mobile.pageLoading();
166- client.del('Account', $j('#accountdetail').find('#Id').val()
167- , function(response) {
168- getAccounts(function(){
169- $j.mobile.pageLoading(true);
170- $j.mobile.changePage('#mainpage',"slide",true,true);
171- });
172- });
173- });
174-
175- $j('#editbtn').click(function(e){
176- // Get account fields and show the 'Edit Account' form
177- e.preventDefault();
178- $j.mobile.pageLoading();
179- client.retrieve("Account", $j('#accountdetail').find('#Id').val()
180- , "Name,Id,Industry,TickerSymbol", function(response){
181- $j('#accountform').find('input').each(function() {
182- $j(this).val(response[$j(this).attr("name")]);
183- });
184- $j('#accformheader').html('Edit Account');
185- setButtonText('#actionbtn','Update');
186- $j('#actionbtn')
187- .unbind('click.btn')
188- .bind('click.btn',updateHandler);
189- $j.mobile.pageLoading(true);
190- $j.mobile.changePage('#editpage',"slide",false,true);
191- });
192- });
68+ addClickListeners();
19369
19470 $j.mobile.pageLoading();
19571 getAccounts(function(){
0 commit comments