DOM, jQuery, AJAX, REST Using Kinvey as JS Back-End

Slides:



Advertisements
Similar presentations
JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Advertisements

SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQuery Web Services A web service is a software system that supports interaction (requesting data,
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
HTTP, AJAX and REST How HTTP and Web Services Work? SoftUni Team Technical Trainers Software University
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
WWW, HTTP, GET, POST, Cookies Svetlin Nakov Telerik Corporation
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
The HTTP Protocol HTTP, Requests, Responses, Forms, MIME, Caching, Redirects SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
MVC Concepts Basics Model-View-Controller (MVC) Concepts for Web Developers SoftUni Team Technical Trainers Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
JavaScript Basics Course Introduction Svetlin Nakov Technical Trainer Software University
Trainers Team Ivan Yonkov Rated in the top 7% at Stack Overflow
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
Part 2 – WWW and HTTP Nikolay Kostov Telerik Corporation
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
Web Fundamentals (HTML and CSS) Course Introduction SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
AJAX in ASP.NET MVC AJAX, Partial Page Rendering, jQuery AJAX, MVC AJAX Helpers SoftUni Team Technical Trainers Software University
Web Fundamentals (HTML and CSS) Course Introduction Svetlin Nakov Technical Trainer Software University
HTML Forms Forms, Controls, Fields, Inputs, Submission, Validation SoftUni Team Technical Trainers Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Web Technologies Lecture 1 The Internet and HTTP.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Web Fundamentals (HTML and CSS)
ASP.NET SignalR SoftUni Team Technical Trainers Software University
JQuery Overview Unleash the Power of jQuery SoftUni Team Technical Trainers Software University
RESTful Web Services What is RESTful?
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
Bootstrap Front-End Framework for Responsive Web Sites Svetlin Nakov Technical Trainer Software University
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Software Technologies Course Overview SoftUni Team Technical Trainers Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Advanced JavaScript Course Introduction SoftUni Team Technical Trainers Software University
National College of Science & Information Technology.
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
WordPress Introduction
JavaScript Applications
JavaScript Applications
State Management Cookies, Sessions SoftUni Team State Management
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
Creating React Components with JSX and Babel
MVC Architecture, Symfony Framework for PHP Web Apps
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
COMP2322 Lab 2 HTTP Steven Lee Feb. 8, 2017.
Extending functionality using Collections
ASP.NET REST Services SoftUni Team ASP.NET REST Services
Introduction Web Environments
Software Quality Assurance
Iterators and Generators
HTTP, RESTful Web Services, HTTP and REST Tools: Postman, Fiddler
WEB API.
Presentation transcript:

DOM, jQuery, AJAX, REST Using Kinvey as JS Back-End DOM, jQuery and AJAX DOM, jQuery, AJAX, REST Using Kinvey as JS Back-End jQuery and AJAX SoftUni Team Technical Trainers Software University http://softuni.bg

Table of Contents Introduction to DOM jQuery Basics HTTP Basics RESTful Services AJAX and jQuery AJAX Using Kinvey as JS Back-End

Have a Question? sli.do #4712

Document Object Model (DOM) DOM represents HTML document as a tree of elements

Problem: Show More Text Create HTML page holding short text + link "Read more …" Clicking on the link shows more text and hides the link

Solution: Show More Text Welcome to the "Show More Text Example". <a href="#" id="more" onclick="showText()">Read more …</a> <span id="text" style="display:none">Welcome to …</span> <script> function showText() { document.getElementById('text') .style.display = 'inline'; document.getElementById('more') .style.display = 'none'; } </script> See the DOM tree here: http://software.hixie.ch/utilities/js/live-dom-viewer/?saved=4275

Problem: List of Items Create a HTML page holding a list of items + text box + button for adding more items to the list

Solution: List of Items <h1>List of Items</h1> <ul id="items"><li>First</li><li>Second</li></ul> <input type="text" id="newItemText" /> <input type="button" onclick="addItem()" value="Add"> <script> function addItem() { let text = document.getElementById('newItemText').value; var li = document.createElement("li"); li.appendChild(document.createTextNode(text)); document.getElementById("items").appendChild(li); } </script>

jQuery jQuery is a cross-browser JavaScript library Dramatically simplifies DOM manipulation Simplifies AJAX calls and working with RESTful services Free, open-source software: https://jquery.com <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> Load jQuery from its official CDN $('li').css('background', '#DDD'); Change the CSS for all <li> tags

Problem: Selectable Towns Create a HTML page listing towns Clicking on a town should select / deselect it A button shows all selected towns <html><head> <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script><style>li { … }</style></head> … <ul id="items"><li>Sofia</li><li>Varna</li>…</ul>

Solution: Selectable Towns Attach JS event handler for each <li> element <script> $('#items li').click(function() { let li = $(this); if (li.attr('data-selected')) { li.removeAttr('data-selected') li.css('background', '') } else { li.attr('data-selected', 'true') li.css('background', '#DDD') } }) </script> $(this) holds the clicked <li> Attach attribute 'data-selected' = 'true' to each selected <li>

Solution: Selectable Towns <button id="showTownsButton">Show Towns</button> <script> $('#showTownsButton').click(function() { let selectedLi = $('#items li[data-selected=true]') let towns = selectedLi.map((i, x) => x.innerText) .get().join(', ') let townsDiv = $('<div>').text("Selected towns: " + towns) $('body').append(townsDiv) }) </script> Select innerText from all <li> Append a new <div> at the end of the HTML body

Playing with DOM and jQuery Live Exercises in Class (Lab)

HTTP Basics HTTP (Hyper Text Transfer Protocol) Text-based client-server protocol for the Internet For transferring Web resources (HTML files, images, styles, etc.) HTTP request HTTP response Web Server

HTTP Request Methods HTTP defines methods to indicate the desired action to be performed on the identified resource Method Description GET Retrieve / load a resource POST Create / store a resource PUT Update a resource DELETE Remove a resource PATCH Update resource partially HEAD Retrieve the resource's headers

HTTP GET Request – Example Example of HTTP GET request: HTTP request line GET /courses/javascript HTTP/1.1 Host: www.softuni.bg Accept: */* Accept-Language: bg Accept-Encoding: gzip, deflate Mozilla/5.0 (Windows NT 10.0) Chrome/51.0.2704.103 Connection: Keep-Alive Cache-Control: no-cache <CRLF> HTTP headers The request body is empty

HTTP POST Request – Example Example of HTTP POST request: HTTP request line POST /webmail/login.phtml HTTP/1.1 Host: www.abv.bg Accept: */* Accept-Language: bg Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0(compatible;MSIE 6.0; Windows NT 5.0) Connection: Keep-Alive Cache-Control: no-cache Content-Length: 59 <CRLF> LOGIN_USER=mente DOMAIN_NAME=abv.bg LOGIN_PASS=top*secret! HTTP headers The request body holds the submitted data

HTTP Response – Example Example of HTTP response from the Web server: HTTP response status line HTTP/1.1 200 OK Date: Fri, 17 Jul 2010 16:09:18 GMT+2 Server: Apache/2.2.14 (Linux) Accept-Ranges: bytes Content-Length: 84 Content-Type: text/html <CRLF> <html> <head><title>Test</title></head> <body>Test HTML page.</body> </html> HTTP response headers HTTP response body

HTTP Tools for Developers Chrome Developer Tools Fiddler

HTTP Tools for Developers Postman REST Client

REST and RESTful Services Representational State Transfer (REST) Architecture for client-server communication over HTTP Resources have URI (address) Can be created / retrieved / modified / deleted / etc. RESTful API / RESTful Service Provide access to server-side resources via HTTP and REST

REST and RESTful Services – Example Create a new post POST http://some-service.org/api/posts Get all posts / specific post GET http://some-service.org/api/posts GET http://some-service.org/api/posts/17 Delete existing post DELETE http://some-service.org/api/posts/17 Replace / modify existing post PUT / PATCH http://some-service.org/api/posts/17

Kinvey as JS Back-End Kinvey is a Mobile Back-End as a Service (mBaaS) Holds your JS / mobile app data in the cloud Anyone can register and create app Kinvey apps hold users and user data Users (API for creating an account) Data collections (CRUD operations) Files (upload / download / delete)

Kinvey: Registration Create a developer account in Kinvey https://console.kinvey. com/sign-up

Kinvey: Create an App Backend Create an app backend (e.g. MyTestApp) https://console.kinvey. com/apps/new You get APP ID + secret keys (admin account)

Kinvey: Create a User Create a user (e.g. guest / guest)

Kinvey: Create a Data Collection Create a data collection (e.g. posts)

Kinvey: Create Data Columns Create some data columns for the posts, e.g. title and body

Kinvey: Create Data Rows Create some data rows for the posts collection

Test Your Backend with Postman Basic authentication user: guest pass: guest https://baas.kinvey.com/appdata/{app_id}

Kinvey and Postman: List Posts URL: https://baas.kinvey.com/appdata/{app_id}/posts Method: GET Authentication: Basic User: guest Pass: guest

Kinvey and Postman: Create a New Post URL: https://baas.kinvey.com/appdata/{app_id}/posts Method: POST Authentication: Basic User: guest Pass: guest Request body title: New Title body: new post body

AJAX (Asynchronous JavaScript and XML) AJAX allows the Web browser (JS client app) To load server-side resource asynchronously Invoke RESTful service and show server data Two types of AJAX Partial page rendering Load HTML fragment and show it in a <div> REST service call Load JSON object from RESTful service and display it with JS

Using AJAX with jQuery $.ajax({ method: "GET", url: "https://some_service/…", headers: { … }, data: { … }, success: function(data, status) { $('body').append(JSON.stringify(data)); }, error: function(data, status) { } });

Problem: List the Posts from Kinvey List all posts from Kinvey posts collection Use jQuery AJAX call to Kinvey

Solution: Call Kinvey with AJAX and jQuery <button id="loadPosts">Load Posts from Kinvey</button> <script> $('#loadPosts').click(function() { let USERNAME = "guest", PASSWORD = "guest" let authBase64 = btoa(USERNAME + ":" + PASSWORD) $.ajax({ method: "GET", url: "https://baas.kinvey.com/appdata/kid_rkcLxcUr/posts", headers: { "Authorization": "Basic " + authBase64 }, success: showPosts, error: showError }); </script>

Solution: Call Kinvey with AJAX and jQuery (2) function showPosts(data, status) { let ul = $('<ul>') for (let post of data) { ul.append($('<li>').text( post.title + " -> " + post.body)); } $('body').append(ul); function showError(data, status) { let errorMsg = "Error: " + JSON.stringify(data); $('body').append($('<div>').text(errorMsg));

Problem: Create a New Post in Kinvey Create a new post in the Kinvey posts collection Use jQuery AJAX access Kinvey

Solution: Create a New Post in Kinvey $('#loadPosts').click(function() { let USERNAME = "guest", PASSWORD = "guest" let authBase64 = btoa(USERNAME + ":" + PASSWORD) let postData = { title: $('#newPostTitle').val(), body: $('#newPostBody').val() } $.ajax({ method: "POST", url: "https://baas.kinvey.com/appdata/kid_rkcLxcUr/posts", headers: { "Authorization": "Basic " + authBase64 }, data: postData, success: showSuccess, error: showError });

Solution: Create a New Post in Kinvey (2) Title: <input type="text" id="newPostTitle" /><br> Body: <input type="text" id="newPostBody" /><br> <button id="loadPosts">Create New Post</button> function showSuccess(data, status) { let responseMsg = "Created: " + JSON.stringify(data); $('body').append($('<div>').text(responseMsg)); } function showError(data, status) { let errorMsg = "Error: " + JSON.stringify(data); $('body').append($('<div>').text(errorMsg));

Summary Documents in the Web browser are represented as DOM tree JS code can traverse / edit the DOM tree jQuery simplifies the DOM manipulations Simplifies AJAX requests AJAX accesses remote data via HTTP AJAX invokes remote RESTful services jQuery AJAX allows HTTP GET / POST / …

jQuery and AJAX https://softuni.bg/courses/software-technologies © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.