How to build PhoneGap + StackMob apps without Backbone.js? - html

I want to build a PhoneGap HTML5 app with a StackMob backend. There seems to be a shortage of books, videos, and tutorials on the topic.
Specifically, how can I build a Phonegap + StackMob app without using Require.js and Backbone.js?

I think stackmob developer website: https://developer.stackmob.com/ is the best resource.

Using phoneGap with StackMob is independent of using Backbone.js and Require.js. The StackMob SDK is built using Backbone.js for managing models and collections.
So, if you want to build an app without Backbone.js, you can make bare AJAX calls to StackMob. Here is a JSFiddle showing how.
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);
}
});
Regarding phoneGap, you'll want to look at the following docs.
https://developer.stackmob.com/js-sdk/using-the-js-sdk-with-phonegap-guide
I've used Adobe phoneGap build successfully.
Btw- I am the Platform Evangelist at StackMob

Related

Export Parse.com class with Cloud Code

How can I create a javascript function to put in Cloud Code of parse.com, which downloads the content of the class in a json file?
I've read through the Cloud Code Documentation at parse.com https://www.parse.com/docs/cloud_code_guide but I haven't found any examples similar to what I need to do.
You can use this for example (there a few ways). Remove // to limit the number of objects (2 or whatever). "english" being my class.
Then you need to read this function from your platform (mobile desktop etc) which you also need to know how to do. What platform do you use? I only know Corona SDK.
Parse.Cloud.define("getall", function(request, response) {
var query = new Parse.Query("english");
//query.limit(2)
query.find({
success: function(results) {
response.success(results);
},

"No 'Access-Control-Allow-Origin'" Error while sending JSON Request to Shopify

I have Shopify account. In the Admin panel, Admin can add any number of products into Shopify store.
But I need to register Custom Product by the public user from my Shopify Store through the Client side.
i have tried the following code.
$('#send').click(function() {
$.ajax({
/* xxxx... : Api key
yyyy... : password */
url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyy#testing.myshopify.com/admin/orders.json',
type: 'POST',
dataType: 'json',
data: {
"product": {
"title": "Burton Custom Freestlye 151",
"body_html": "<strong>Good snowboard!</strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"tags": "Barnes & Noble, John's Fav, \"Big Air\""
}
},
success: function(response) {
alert(response);
}
});
});
I am getting the following error:
XMLHttpRequest cannot load https://testing.myshopify.com/admin/orders.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://testing.myshopify.com' is therefore not allowed access.
#Deena. In case you missed it this use of Javascript is horribly insecure.
You are making your API keys public and hence anyone can alter or erase your entire shop.
Instead create a private App that you can properly control security wise, and use Javascript in a controlled fashion without exposing your API keys to the world.
You will be amazed at how quickly your reputation will be destroyed if you deploy unsafe code like this and your client ends up responsible for very bad things happening.
For anyone else drifting into this topic, your front-end JS can do this by calling an App Proxy, providing security and the ability to muck about with the RestAPI or the GraphQL approach.
The problem here is that your origin is insecure (http://) and resource is secure (https://). This causes the browser to treat them as different domains and since your resource (orders.json) doesn't specify that requests from http:// version of the URL should be allowed, the request fails, because by default cross-domain requests are forbidden for security considerations. You should be able to run the same script, if you access the page where your script runs via https://.

Dojo 1.9 JsonRest remote url on a different server

I'm trying to get dojo to show Json data that comes from a remote web service. I need to be clear though - the web server hosting the html/dojo page I access isn't the same server as the one that's running the web service that returns the json data - the web service server just can't serve html pages reliably (don't ask!!).
As a test I move the page into the same web server as the web service and the below works. As soon as I move it back so that the html/dojo is served from Apache (//myhost.nodomain:82 say) and the web service sending the json is "{target:http://myhost.nodomain:8181}", then it stops working.
I've used FFox to look at the network & I see the web service being called ok, the json data is returned too & looks correct (I know it is from the previous test), but the fields are no longer set. I've tried this with DataGrid and the plain page below with the same effects.
Am I tripping up over something obvious???
Thanks
require([
"dojo/store/JsonRest",
"dojo/store/Memory",
"dojo/store/Cache",
"dojox/grid/DataGrid",
"dojo/data/ObjectStore",
"dojo/query",
"dojo/domReady!"
],
function(JsonRest, Memory, Cache, DataGrid, ObjectStore, query) {
var myStore, dataStore, grid;
myStore = JsonRest(
{
target: "http://localhost:8181/ws/job/definition/",
idProperty: "JOB_NAME"
}
);
myStore.query("JOB00001"
).then(function(results) {
var theJobDef = results[0];
dojo.byId("JOB_NAME").innerHTML = theJobDef.JOB_NAME;
dojo.byId("SCHEDULED_DAYS").innerHTML = theJobDef.SCHEDULED_DAYS;
});
}
);
Its true what Frans said about the cross domain restriction but dojo has this link to work around the problem.
require(["dojo/request/iframe"], function(iframe){
iframe("something.xml", {
handleAs: "json"
}).then(function(xmldoc){
// Do something with the XML document
}, function(err){
// Handle the error condition
});
// Progress events are not supported using the iframe provider
});
you can simply use this and the returned data can be inserted into a store and then into the grid.
Are you familiar with the Same Origin Policy:
http://en.wikipedia.org/wiki/Same-origin_policy
Basically it restricts websites to do AJAX requests to other domains than the html page was loaded from. Common solutions to overcome this are CORS and JSON-P. However, remember that these restrictions are made for security reasons.

Face Recognition in Chrome extension

I want to create a Chrome browser extension which would be using Face Recognition algorithms to customize personal bookmarks, passwords and themes.
Is there any possibility to use different technologies then JavaScript in this case?
You can use Native Client (NaCl) to compile C/C++ code into a form that will run safely inside Chrome. This is feasible as long as the NaCl runtime (provided by the Pepper API) meets your needs. You will communicate with your NaCl module with Javascript in your application.
I created a Javascript face recognition tutorial here - http://jsfiddle.net/ismaelc/fgq7L/ .(Disclaimer: I work for Mashape). It's using photobooth.js to capture images, and using Lambda's Face recognition API (https://www.mashape.com/lambda/face-recognition). I am calling the API like this:
$.ajax({
url: "https://lambda-face-recognition.p.mashape.com/recognize",
type: "POST",
data: fd,
processData: false,
contentType: false,
beforeSend: function (xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", $("#mashapeKey").val());
}
}).done(function (result) {
alert("Received response..");
var resultObject = JSON.stringify(result);
alert(resultObject);
});
You might also want to check this demo that uses the Skybiometry API (https://www.mashape.com/skybiometry-1/skybiometry-face-detection-and-recognition#!documentation) with webRTC - http://fadomire.futureauth.jit.su/
You can also check out this list of 40+ face recognition APIs, libraries, software - http://blog.mashape.com/post/53379410412/list-of-40-face-detection-recognition-apis
No, extensions have to be created using HTML and JavaScript.

jQuery Twitter API returning 403 forbidden on server

I've been working on a site that implements Twitter's API using jQuery's $.ajax function. It works when I run it on my machine, but when I upload it to my server and run it, it returns at 403: forbidden every time. I definitely haven't exceeded their rate limit, so I don't think I've been blacklisted. A few of my colleagues have developed similar problems in their pages since around midnight last night. Does anyone know if Twitter is changing their API? Have they depreciated the json requests?
Here is my code for those interested:
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://search.twitter.com/search.json?q=not+art&rpp=100&callback=?",
dataType: "json",
success: function(data) {
d = data.results;
for(i=0;i<d.length;i++){
var idStr = d[i].id_str;
console.log(i+" = "+idStr);
}
}
});
});
I'm fairly certain it's not a code issue since it still works running offline, but there isn't any word from twitter on recent changes that I can find. Any help or info would be greatly appreciated. Are we all going to have to learn a new API?
EDIT 3/29/13
The problem appears to be connected to using a shared IP address to access the search.twitter.com API. The Twitter Developer forums didn't have any more information than that, unfortunately, but if you are having similar problems, look into that.