如何在没有Backbone.js的情况下构建PhoneGap + StackMob应用程序?

时间:2013-07-26 15:12:18

标签: html5 cordova stackmob

我想用StackMob后端构建PhoneGap HTML5应用程序。关于该主题的书籍,视频和教程似乎不足。

具体来说,如何在不使用Require.js和Backbone.js的情况下构建Phonegap + StackMob应用程序?

2 个答案:

答案 0 :(得分:1)

我认为stackmob开发者网站:https://developer.stackmob.com/是最好的资源。

答案 1 :(得分: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。

顺便说一下 - 我是StackMob的平台布道者