AngularJS PART 2 - services and data from database examples

Slides:



Advertisements
Similar presentations
Introduction to Arc GIS. Finding information graphically.
Advertisements

Application Graphic design / svetagraphics.com 01 FRAMEWORK data service.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
06 | Implementing Web APIs Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.
1 Review of Important Networking Concepts Introductory material. This slide uses the example from the previous module to review important networking concepts:
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation MongoDB Read Lecturer.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
Server-side Scripting Powering the webs favourite services.
Lecturer: Ghadah Aldehim
Painless Bug Tracking Michael Tsai 2011/9/30. Reference  html 2.
THE BIG PICTURE. How does JavaScript interact with the browser?
The New Computing Curriculum select, use and combine a variety of software (including internet services) on a range of digital devices to design and create.
MVC CompSci 230 S Software Construction. MVC Architecture  A typical application includes software to  maintain application data,  document text.
Android - Project Green basket Android Application * Let's you do your grocery shopping location based. * Let's you decide to go to the closest grocery.
 The tag to create a link is called, which stands for anchor.  You put the address of the page to link to in quotes after href=, like the following:
SE-2840 Dr. Mark L. Hornick1 MongoDB A persistent data store.
Mainframe (Host) - Communications - User Interface - Business Logic - DBMS - Operating System - Storage (DB Files) Terminal (Display/Keyboard) Terminal.
1 UNIT 13 The World Wide Web Lecturer: Kholood Baselm.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Ten Commandments of Word Processing. I. Thou shall not use spaces n Put no more than two spaces together. n Use the key to line things up. n Better yet,
Node.Js 1. 2 Contents About Node.Js Web requirement latest trends Introduction Simple web server creation in Node.Js Dynamic Web pages Dynamic web page.
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
1 UNIT 13 The World Wide Web. Introduction 2 Agenda The World Wide Web Search Engines Video Streaming 3.
1 UNIT 13 The World Wide Web. Introduction 2 The World Wide Web: ▫ Commonly referred to as WWW or the Web. ▫ Is a service on the Internet. It consists.
START Application Spencer Johnson Jonathan Barella Cohner Marker.
Ajax 101 for CF Programmers Randy Brown – Michigan State University
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Lesson 11: Web Services and API's
JavaScript Basics Stephen Delaney
Introduction to MEAN (JavaScript Full Stack Development)
NodeJS and MEAN cs6320.
Tonga Institute of Higher Education IT 141: Information Systems
The STEM Academy Data Solution
Web Application.
FOP: JavaScript Arrays(Lists)
CS 371 Web Application Programming
Ad-blocker circumvention System
Harry Williams, Cartography
Play Framework: Introduction
NodeJS and MEAN Prof. L. Grewe.
Web Software Model CS 4640 Programming Languages for Web Applications
How to fix YouTube error 400? Call us 0 (800)
AngularJS intro L. Grewe.
CMPE 280 Web UI Design and Development October 24 Class Meeting
Writing JavaScript Code
CHAPTER 3 Architectures for Distributed Systems
Network Programming Lecture 4: Message Posting
Angularjs Interview Questions and Answers By Hope Tutors.
MVC Framework, in general.
Offline Database Synchronization with SOAP and MySQL
ISC440: Web Programming 2 Server-side Scripting PHP 3
NodeJS coding basics L. Grewe.
AngularJS intro L. Grewe.
Tonga Institute of Higher Education IT 141: Information Systems
Lesson 16: Functions with Return Values
Lecture 1: Multi-tier Architecture Overview
Refactoring Legacy AngularJS
CMPE 280 Web UI Design and Development October 18 Class Meeting
Tonga Institute of Higher Education IT 141: Information Systems
Web Programming Language
Sending a text message (and more)
Lesson 11: Web Services and API's
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Chengyu Sun California State University, Los Angeles
Server-Side Programming
Chengyu Sun California State University, Los Angeles
Message Passing Systems
Presentation transcript:

AngularJS PART 2 - services and data from database examples L. Grewe

Adding dynamic data to our loc8r application From hard coding to

First lets create a /getAllLocations

Routing in index.js maps to controller in database.js file var controllerMongoCollection = require('../controllers/database’); //************************************************************************** //***** mongodb get all of the Locations in the LOCATIONS collection // and sort by the name of the route. Render router.get('/getAllLocations', controllerMongoCollection.getAllLocations);//end app.get

Now the controller code in controllers/database.js var mongodb = require('mongodb'); var mongoDBURI = process.env.MONGODB_URI || 'mongodb://XXXX.mlab.com:53239/YY’; module.exports.getAllLocations = function (request, response) { mongodb.MongoClient.connect(mongoDBURI, function(err, db) { if(err) throw e //get collection of Locations var Locations = db.collection('LOCATIONS'); //get all Locations Locations.find().toArray(function (err, docs) { if(err) throw err; response.render('getAllLocations', {results: docs}); }); //close connection when your app is terminating. db.close(function (err) { if(err) throw err; }); });//end of connect };//end function

View views/getAllLocations We will get rid of this view later when we modify the controller to simply return the results for use in an ANGULAR output –so lets ignore it for now but, we have seen the output

LETS instead respond with json response module.exports.getAllLocationsData = function (request, response) { mongodb.MongoClient.connect(mongoDBURI, function(err, db) { if(err) throw e //get collection of orders var Locations = db.collection('LOCATIONS'); //get results of ALL locations with using the toArray function Locations.find().toArray(function (err, docs) { if(err) throw err; //Instead of sending it to an ejs for rending //simply return the array of JSON documents response.json(docs); //send as JSON documents response }); //close connection when your app is terminating. db.close(function (err) { if(err) throw err; }); });//end of connect };//end function Controller code in controllers/database.js

Back to Angular We are going to create a service

Angular Service self-contained units of functionality that can be combined to provide the complete functionality of a software application. BEST PRACTICE: in Angular most application logic should be deferred to services, making it reusable from multiple controllers

An Angular service to access data –edit the public/angular/loc8rapp.js angular.module("loc8rApp", []); var loc8rData = function () { return [{ name: 'Burger Queen', address: '125 High Street, Reading, RG6 1PS', rating: 3, facilities: ['Hot drinks', 'Food', 'wifi'], distance: '0.296456', _id: '5370a35f2536f6785f8dfb6a' },{ name: 'Open Internet Cafe', address: '12 A Street, Hayward, CA', rating: 5, facilities: ['Hot drinks', 'Food', 'wifi'], distance: '0.3864323', _id: '51230a35f2536f6785f8dfb6a' }, { name: 'Costy', address: '125 High Street, Reading, RG6 1PS', rating: 5, facilities: ['Hot drinks', 'Food', 'Alcoholic drinks'], distance: '0.7865456', _id: '5370a35f2536f6785f8dfb6a' }]; };

CONTINUED……..public/angular/loc8rapp.js //define application to use service angular .module('loc8rApp’) .service('loc8rData’ , loc8rData ); //getting data from a service var locationListCtrl = function ($scope, loc8rData ) { $scope.data = { locations: loc8rData }; }; //define application to use controller angular .module('loc8rApp') .controller('locationListCtrl', locationListCtrl) .filter('formatDistance', formatDistance) .directive('ratingStars', ratingStars) .directive('ratingStars2', ratinginStars2) .service('loc8rData', loc8rData);

Output same and STILL have Hardcoded the data but, NOW in the service

Want AngularJS Service code to be able to use our /getAllLocations URI—get data we use the BUILT IN Angluar $http service CHANGE our previously defined service function loc8rData function in the public/angular/loc8rapp.js file var loc8rData = function ($http) { return $http.get(‘/getAllLocations’); } THIS CODE WON’T WORK - see next slide

BUT what happened --- I GET NOTHING NOW? ---lets fix this THE ANSWER is --- the $http.get method in your service function loc8rData is ASYNCHROUNOUS and takes time so simply having your controller try to use the service method directly WONT work you need to HAVE A success callback function In the public/angular/loc8rapp.js angular code locationListCtrl = function ($scope, loc8rData) { loc8rData .success(function(data) { $scope.data = { locations: data }; }) .error(function (e) { console.log(e); }); };