2013-07-26 69 views

回答

1

將phoneGap與StackMob結合使用獨立於使用Backbone.js和Require.js。 StackMob SDK是使用Backbone.js構建的,用於管理模型和集合。

所以,如果你想建立一個沒有Backbone.js的應用程序,你可以對StackMob進行裸露的AJAX調用。這是一個展示如何的JSFiddle。

http://jsfiddle.net/ericktai/mr925/

/* 
We want to prepare the Request headers we're going to send to StackMob. It should look like: 

{ 
    'Accept': application/vnd.stackmob+json; version=0', 
    'X-StackMob-API-Key-dc0e228a-ccd3-4799-acd5-819f6c074ace': 1, 
    'Range': 'objects=0-9' //this is optional, but I did it here to show pagination and extra header fields 
} 

You can actually have the public key in the header as: 

'X-StackMob-API-Key': dc0e228a-ccd3-4799-acd5-819f6c074ace 

OR 

'X-StackMob-API-Key-dc0e228a-ccd3-4799-acd5-819f6c074ace': 1 

The first is our original format. The reason why I chose the latter is because of this specific example. I'm making cross domain requests jsfiddle.net to api.stackmob.com, which the browser doesn't allow UNLESS we use CORS (cross origin resource sharing). StackMob servers support CORS, but it needs the latter header format to do so. (it was introduced later). iOS and Android SDKs use the first format. 

Node.js should be able to use the first format because it doesn't restrict cross domain calls. 

The "1" value in the latter format is arbitrary. IE just doesn't allow the value of a header to be empty, so we filled it with "1". 
*/ 

var publicKeyHeader = 'X-StackMob-API-Key-dc0e228a-ccd3-4799-acd5-819f6c074ace'; 
var requestHeaders = {}; 
requestHeaders['Accept'] = 'application/vnd.stackmob+json; version=0'; 
requestHeaders[publicKeyHeader] = 1; 
requestHeaders['Range'] = 'objects=0-9'; //set pagination to first 10 

$.ajax({ 
    url: 'https://api.stackmob.com/item', 
    headers: requestHeaders, //set the headers 
    type: 'GET', 
    success: function(data, textStatus, xhr) { 
     console.debug(data); 
    }, 
    error: function(xhr, textStatus, error) { 
     console.debug(error); 
    } 
}); 

關於PhoneGap的,你會想看看下面的文檔。

https://developer.stackmob.com/js-sdk/using-the-js-sdk-with-phonegap-guide

我使用的Adobe PhoneGap的成功打造。

Btw-我是StackMob的平臺推廣者

+0

上面是一個「fetch」調用,實現對我們的REST API,這是記錄在這裏:https://developer.stackmob.com/rest-api/api-docs(我也爲StackMob工作) –

相關問題