Parse Lecture - 6.470

Parse
Jamie Karraker ’12 MEng ’13
no
no
no
Z
Z
Z
+
+
REST API
Database
yes!
+
+
Networking
Server
no
no
+ users
+ security
Caching
The fun stuff!
JavaScript SDK
iOS
Android
OS X
WP8
Win 8
.NET
Unity
REST
The
USERS
in your app
Sign up a user
Parse.User.signUp("TimBeaver", "ihtfp");
!
!
!
!
!
!
Log in a user
Parse.User.logIn("TimBeaver", "ihtfp");
!
!
!
!
!
!
Sign up a user
Parse.User.signUp("TimBeaver", "ihtfp", {
success: function(user) {
// Do stuff after successful signup.
},
error: function(user, error) {
// The login failed. Check error to see why.
}
});
Log in a user
Parse.User.logIn("TimBeaver", "ihtfp", {
success: function(user) {
// Do stuff after successful login.
},
error: function(user, error) {
// The login failed. Check error to see why.
}
});
Facebook Login
Parse.FacebookUtils.logIn(null, {
success: function(user) {
if (!user.existed()) {
// User signed up and logged in through Facebook!
} else {
// User logged in through Facebook!
}
},
error: function(user, error) {
// User cancelled the Facebook login or did not fully authorize.
}
});
Link Existing User
if (!Parse.FacebookUtils.isLinked(user)) {
Parse.FacebookUtils.link(user, null, {
success: function(user) {
// Woohoo, user logged in with Facebook!
},
error: function(user, error) {
// User cancelled the Facebook login or did not fully authorize.
}
});
}
DATA
Saving stuff in the
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.set("score", 1337);
gameScore.set("player", Parse.User.current());
gameScore.set("cheatMode", false);
gameScore.save();
FILES
Saving your
Pictures
in the
HTML
!
<input type="file" id="profilePhotoFileUpload">
Save a File
!
var file = $("#profilePhotoFileUpload")[0].files[0];
var name = "photo.jpg";
var parseFile = new Parse.File(name, file);
parseFile.save(null, {
success: function(parseFile) {
profileObject.set("photoFile", parseFile);
profileObject.save();
}
});
Retrieve a File
var profilePhoto = profileObject.get("photoFile");
$("#profileImg")[0].src = profilePhoto.url();
DATA BROWSER
Because sometimes you just want to see it
QUERIES
Getting stuff from the
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
!
query.equalTo("player", Parse.User.current());
!
query.find();
!
!
!
!
!
!
!
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
!
query.equalTo("player", Parse.User.current());
!
query.find({
success: function(gameScores) {
// Do something with the returned Parse.Object array
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
RELATIONS
It’s better together
var gameScore = new GameScore();
gameScore.set("player", Parse.User.current());
var user = Parse.User.current();
var relation = user.relation("friends");
relation.add(newFriend);
relation.add(anotherFriend);
user.save();
CLOUD CODE
It’s code in the
Before Save
Parse.Cloud.beforeSave("Review", function(request, response) {
var comment = request.object.get("comment");
if (comment.length > 140) {
// Truncate and add a ...
request.object.set("comment", comment.substring(0, 137) + "...");
}
response.success(); });
After Save
Parse.Cloud.afterSave("Review", function(request) {
query = new Parse.Query("Movie");
query.get(request.object.get("movie").id, {
success: function(movie) {
movie.increment("reviews");
movie.save();
}
});
});
Cloud Modules
Stripe
var Stripe = require('stripe');
Stripe.initialize('mySecretKey');
!
Stripe.Charges.create({
amount: 100 * 10, // $10 expressed in cents
currency: "usd",
card: "tok_3TnIVhEv9P24T0" // stripe token id
});
PARSE HOSTING
Overview
Static Hosting
Dynamic Hosting
Any Meme Lite Example
The Spectrum
The Spectrum
The Spectrum
PARSE HOSTING
Static Hosting
•
•
•
•
•
Choose a domain:
myapp.parseapp.com (or custom)
Initialize your parse directory
Create public/helloword.html
Type parse deploy
Your web page is live
Live Demo: Todos
Dynamic Hosting
express.js
express.js
•
•
•
•
Web framework built for node.js
Node.js is server-side JavaScript
Now with Cloud Code + Parse JS SDK
Simple but expressive
Directory Structure
-cloud/
main.js app.js -views/ hello.ejs
-public/
example.html -stylesheets/ style.css
Cloud Code
Express code
View templates
Static HTML files
CSS stylesheets
Workflow
•
Install Command Line Tool at
https://www.parse.com/docs/cloud_code_guide#started
$ curl -s https://www.parse.com/downloads/cloud_code/installer.sh |
sudo /bin/bash
•
One time set up. Create a local code directory
$ parse new MyWebsite
•
•
Make code changes
$ vi MyWebsite/cloud/app.js
Deploy to the cloud
$ parse deploy
Live Demo: AnyMeme
What Else?
•
•
•
•
•
Full blown web apps
Admin interfaces
Web hooks
Push Notifications
Analytics
Further Reading
•
•
•
•
•
•
•
JS SDK guide: parse.com/docs/js_guide
Hosting guide: parse.com/docs/hosting_guide
Express: expressjs.com
Anymeme: www.anymeme.org
Todo App: todolist.parseapp.com
Tutorials: parse.com/tutorials
Source code: github.com/ParsePlatform