Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Your Website Faster With Parse

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Building Your Website Faster With Parse

Avatar for zaoldyeck

zaoldyeck

July 10, 2015

Other Decks in Programming

Transcript

  1. What is Parse ? • Facebook Clould Service • Handle

    database for you. • Handle host for you. • Handle execute code for you. • It’s suitable for building prototype. • Free if requests less than 30 times per second.
  2. How to use it ? 1. Log In the website

    https://www.parse.com 2. Create a new App 3. Add Parse Javascript SDK to your website. 4. Use Parse API right away.
  3. What will you need to use ? Core • Data

    - For storing data. • Clould Code - Run your javascript code on the service. • Logs - See what happend when executing Cloud Code.
  4. Create project 1. $ curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash

    2. $ parse new projectName 3. $ cd projectName 4. $ parse generate express - for dynamic web , otherwise you just can deploy one page.
  5. Project Structure -config/ global.json -cloud/ main.js - Cloud Code should

    write here. -public/ - pages placed here. index.html
  6. Objects - The entity in core data. var GameScore =

    Parse.Object.extend("GameScore"); var gameScore = new GameScore(); gameScore.set("score", 1337); gameScore.set("playerName", "Sean Plott"); gameScore.set("cheatMode", false); gameScore.save().then(function (gameScore) { // Execute any logic that should take place after the object is saved. alert('New object created with objectId: ' + gameScore.id); }, function (error) { // Execute any logic that should take place if the save fails. // error is a Parse.Error with an error code and message. alert('Failed to create new object, with error code: ' + error.message); });
  7. Queries var GameScore = Parse.Object.extend("GameScore"); var query = new Parse.Query(GameScore);

    query.equalTo("playerName", "Dan Stemkoski"); query.find().then(function (results) { alert("Successfully retrieved " + results.length + " scores."); // Do something with the returned Parse.Object values for (var i = 0; i < results.length; i++) { var object = results[i]; alert(object.id + ' - ' + object.get('playerName')); } }, function (error) { alert("Error: " + error.code + " " + error.message); })
  8. Users //Sign Up var user = new Parse.User(); user.set("username", "my

    name"); user.set("password", "my pass"); user.set("email", "[email protected]"); // other fields can be set just like with Parse.Object user.set("phone", "415-392-0202"); user.signUp().then(function (user) { // Hooray! Let them use the app now. }, function (error) { // Show the error message somewhere and let the user try again. alert("Error: " + error.code + " " + error.message); }) //Log In Parse.User.logIn("myname", "mypass").then (function (user) { // Do stuff after successful login. }, function (error) { // The login failed. Check error to see why. }) //Log out if (Parse.User.current()) { Parse.User.logOut() }
  9. Access Control List This object can only be read or

    write by current user. var Note = Parse.Object.extend("Note"); var privateNote = new Note(); privateNote.set("content", "This note is private!"); privateNote.setACL(new Parse.ACL(Parse.User.current())); privateNote.save();
  10. Cloud Code • Put application logic at server side. •

    For security. • It can do background jobs.
  11. Cloud Code //Server Side Parse.Cloud.define("averageStars", function (request, response) { var

    query = new Parse.Query("Review"); query.equalTo("movie", request.params.movie); query.find().then(function (results) { var sum = 0; for (var i = 0; i < results.length; ++i) { sum += results[i].get("stars"); } response.success(sum / results.length); }, function (error) { response.error("movie lookup failed"); }) }); //Client Side Parse.Cloud.run('averageStars', {movie: 'The Matrix'}).then(function (ratings) { // ratings should be 4.5 }, function (error) { alert(error.message); });
  12. Tips • Set the entity can’t be read or write

    by anyone. • Set the entity can only be execute by administrator. • In Cloud Code , use master key to execute the object.
  13. Use Master Key In Cloud Code var User = Parse.Object.extend("User")

    var query = new Parse.Query(User) query.get(request.params.userId, {useMasterKey: true}).then(function (user) { user.set("isTakeLottery", true) user.save(null, {useMasterKey: true}) response.success() }, function (error) { response.error(error.message) })
  14. Deploy $ parse deploy • It will host on http://yousetsubdomain.

    parseapp.com • You can host the application on your own domain.
  15. Shortcommings • Cloud Code can’t run on your machine ,

    you need internet. • Cloud Code has no package manager , like npm. • There are no testing framework on Cloud Code , what you only can use is logs. • If you want distinguish the project between development and production , you need add another project. • There might be cached while deploying newest version of parse application.
  16. Summery • Easy to learn. • Do every back end

    things for you , you only focus on front end. • Don’t use Parse build giant project. • It’s suitable for making prototype or one used project.