SlideShare ist ein Scribd-Unternehmen logo
1 von 13
$RESOURCE
VANCOUVER ANGULARJS – APRIL 2ND, 2014
ABOUT @SACHINKAGRAWAL
 I run EDP Software and we build a product called SchedulePro
 Relatively new to AngularJS. Just now starting to use it in
Production scenarios
 Background
8 years as a Program Manager at Microsoft before EDP Software
University of Waterloo alumni
We’re hiring! http://www.edpsoftware.com/about-edp-software/careers/
Email: hr@edpsoftware.com
GOALS FOR TODAY
 Use $resource for connecting to REST web services
 Discuss how to use $resource in a web application
Separate API details from application
Where to put common tasks like data transforms
 Get feedback from the group on what else could be done better!
MY THOUGHTS ON COMMON EXAMPLES
 Most documentation is too simple
 Logic for using $http or $resource is built right into a controller
 Two big issues that I found
REST API changes are not isolated
No common place to handle async status
$RESOURCE INTRO
 Provides a really simple way to grab data from a REST endpoint
Standard methods for query, get, save, delete
Easy to define urls, parameters, custom additions, etc. (see docs)
 Super simple to get started (you should turn this into a factory)
var ResourceObject = $resource("http://foo/Object/…");
var resultData = ResourceObject.query();
//The following call will be bound to the scope and then UI if rendered
//UI will magically update when the request is complete
$scope.foo = ResourceObject.query();
ASYNCHRONOUS CALLS AND PROMISES
 Angular uses promises heavily in many of the built in APIs
 $q is a lightweight promise implementation
 Promises provide the ability to control and chain requests together.
 NOTE: The results of $resource (or $http) are asynchronous. If your
code expects data immediately after calling a query, you will have an
empty result!!!
 NOTE 2: Angular calls the digest loop after $http calls complete
which is why the UI seems to magically get the data
$HTTP VS. $RESOURCE
PROMISES VS. CALLBACKS
 $http uses promises as the design pattern for managing async
actions
 $resource defaults to using a callback structure
Allow you to access the promise exposed by $http
 HELP Me Vangular!?
Suggestions on which route to choose? I exposed the promise from
$resource in my last example.
CALLBACKS FOR SETTING RESULTS
 BAD – What many base examples show
No way of taking action when the async call completes.
Easy to start with but quickly limits your application
 Good – Use the callback function to set variables on the scope
$scope.foo = ResourceObject.query();
ResourceObject.query(function(results){
$scope.foo = results;
//Do any other actions here….
});
ERROR HANDLING – SECOND CALLBACK
 http://stackoverflow.com/questions/20584367/how-to-handle-
resource-service-errors-in-angularjs
Resource.query(function(data) {
// success handler
}, function(error) {
// error handler
});
CREATING A LAYER OF ABSTRACTION
 Isolate - REST APIs may be out of your control (e.g. 3rd party).
Create a factory with your own API
 Simplify/Transform data
Promises can be chained together
The return result from one promise goes into the next as the incoming
data
EXAMPLE
angular.module('sampleApp').factory('resourceAPI', ['$resource', function ($resource) {
var ResourceObject = $resource("http://foo/Object/…");
return {
getTransformedValues: function () {
return ResourceObject.query().$promise.then(function(data) {
//Make whatever transforms are required
//Underscore library is useful here
data = ....
//The returned data will be the result
//when the promise resolves
return data;
});
}, otherFunctionsInAPI: function() {}
};
} ]);
EXAMPLE PART II
Meanwhile.... In your controller
$scope.showSpinner = true;
resourceAPI.getTransformedValues().then(function(data){
$scope.foo = data;
return data;
}, function(error){
//Perform whatever error handling you wish
})[‘finally’](function(){
//Odd finally syntax is for IE8 compat
$scope.showSpinner = false;
});
FUTURE STUFF TO INVESTIGATE/LEARN
 http interceptors
Retry Logic
Security Scenarios
 Response caching

Weitere ähnliche Inhalte

Was ist angesagt?

Troubleshooting oracle apps
Troubleshooting oracle appsTroubleshooting oracle apps
Troubleshooting oracle apps
vamsi18here
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch
 
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
Jonathan Linowes
 
Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
Eric Hamilton
 

Was ist angesagt? (14)

Deep dive into AngularJs for Beginners
Deep dive into AngularJs for BeginnersDeep dive into AngularJs for Beginners
Deep dive into AngularJs for Beginners
 
Ember.js for Big Profit
Ember.js for Big ProfitEmber.js for Big Profit
Ember.js for Big Profit
 
Web apps without internet
Web apps without internetWeb apps without internet
Web apps without internet
 
Troubleshooting oracle apps
Troubleshooting oracle appsTroubleshooting oracle apps
Troubleshooting oracle apps
 
Andy Bosch - JavaServer Faces in the cloud
Andy Bosch -  JavaServer Faces in the cloudAndy Bosch -  JavaServer Faces in the cloud
Andy Bosch - JavaServer Faces in the cloud
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
Heroku addons development - Nov 2011
Heroku addons development - Nov 2011Heroku addons development - Nov 2011
Heroku addons development - Nov 2011
 
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
Integrating Angular.js with Rails (Wicked Good Ruby Conf lightening talk)
 
Using Angular with Rails
Using Angular with RailsUsing Angular with Rails
Using Angular with Rails
 
Web Application FLow
Web Application FLowWeb Application FLow
Web Application FLow
 
Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
 
Introduction to Spring Cloud Kubernetes (July 4th, 2019)
Introduction to Spring Cloud Kubernetes (July 4th, 2019)Introduction to Spring Cloud Kubernetes (July 4th, 2019)
Introduction to Spring Cloud Kubernetes (July 4th, 2019)
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
 
Lessons from helping developers integrate 1,000 APIs with Zapier
Lessons from helping developers integrate 1,000 APIs with ZapierLessons from helping developers integrate 1,000 APIs with Zapier
Lessons from helping developers integrate 1,000 APIs with Zapier
 

Ähnlich wie Vancouver AngularJS using $resource in your application

Resume_AliceZhou
Resume_AliceZhouResume_AliceZhou
Resume_AliceZhou
Alice Zhou
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 

Ähnlich wie Vancouver AngularJS using $resource in your application (20)

Cqrs api v2
Cqrs api v2Cqrs api v2
Cqrs api v2
 
REST in AngularJS
REST in AngularJSREST in AngularJS
REST in AngularJS
 
Drupalcon Mumbai
Drupalcon MumbaiDrupalcon Mumbai
Drupalcon Mumbai
 
JAX-RS. Developing RESTful APIs with Java
JAX-RS. Developing RESTful APIs with JavaJAX-RS. Developing RESTful APIs with Java
JAX-RS. Developing RESTful APIs with Java
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
 
Laravel + Restangular Introduction
Laravel + Restangular IntroductionLaravel + Restangular Introduction
Laravel + Restangular Introduction
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
 
Day02 a pi.
Day02   a pi.Day02   a pi.
Day02 a pi.
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJS
 
Spring, web service, web server, eclipse by a introduction sandesh sharma
Spring, web service, web server, eclipse by a introduction sandesh sharmaSpring, web service, web server, eclipse by a introduction sandesh sharma
Spring, web service, web server, eclipse by a introduction sandesh sharma
 
SharePoint 2013 REST APIs
SharePoint 2013 REST APIsSharePoint 2013 REST APIs
SharePoint 2013 REST APIs
 
Drupal & AngularJS - DrupalCamp Spain 2014
Drupal & AngularJS - DrupalCamp Spain 2014Drupal & AngularJS - DrupalCamp Spain 2014
Drupal & AngularJS - DrupalCamp Spain 2014
 
Integrate Your Test Automation Tools for More Power
Integrate Your Test Automation Tools for More PowerIntegrate Your Test Automation Tools for More Power
Integrate Your Test Automation Tools for More Power
 
Hue: Big Data Web applications for Interactive Hadoop at Big Data Spain 2014
Hue: Big Data Web applications for Interactive Hadoop at Big Data Spain 2014Hue: Big Data Web applications for Interactive Hadoop at Big Data Spain 2014
Hue: Big Data Web applications for Interactive Hadoop at Big Data Spain 2014
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
Resume_AliceZhou
Resume_AliceZhouResume_AliceZhou
Resume_AliceZhou
 
REST Enabling your Oracle Database (2018 Update)
REST Enabling your Oracle Database (2018 Update)REST Enabling your Oracle Database (2018 Update)
REST Enabling your Oracle Database (2018 Update)
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Vancouver AngularJS using $resource in your application

  • 2. ABOUT @SACHINKAGRAWAL  I run EDP Software and we build a product called SchedulePro  Relatively new to AngularJS. Just now starting to use it in Production scenarios  Background 8 years as a Program Manager at Microsoft before EDP Software University of Waterloo alumni We’re hiring! http://www.edpsoftware.com/about-edp-software/careers/ Email: hr@edpsoftware.com
  • 3. GOALS FOR TODAY  Use $resource for connecting to REST web services  Discuss how to use $resource in a web application Separate API details from application Where to put common tasks like data transforms  Get feedback from the group on what else could be done better!
  • 4. MY THOUGHTS ON COMMON EXAMPLES  Most documentation is too simple  Logic for using $http or $resource is built right into a controller  Two big issues that I found REST API changes are not isolated No common place to handle async status
  • 5. $RESOURCE INTRO  Provides a really simple way to grab data from a REST endpoint Standard methods for query, get, save, delete Easy to define urls, parameters, custom additions, etc. (see docs)  Super simple to get started (you should turn this into a factory) var ResourceObject = $resource("http://foo/Object/…"); var resultData = ResourceObject.query(); //The following call will be bound to the scope and then UI if rendered //UI will magically update when the request is complete $scope.foo = ResourceObject.query();
  • 6. ASYNCHRONOUS CALLS AND PROMISES  Angular uses promises heavily in many of the built in APIs  $q is a lightweight promise implementation  Promises provide the ability to control and chain requests together.  NOTE: The results of $resource (or $http) are asynchronous. If your code expects data immediately after calling a query, you will have an empty result!!!  NOTE 2: Angular calls the digest loop after $http calls complete which is why the UI seems to magically get the data
  • 7. $HTTP VS. $RESOURCE PROMISES VS. CALLBACKS  $http uses promises as the design pattern for managing async actions  $resource defaults to using a callback structure Allow you to access the promise exposed by $http  HELP Me Vangular!? Suggestions on which route to choose? I exposed the promise from $resource in my last example.
  • 8. CALLBACKS FOR SETTING RESULTS  BAD – What many base examples show No way of taking action when the async call completes. Easy to start with but quickly limits your application  Good – Use the callback function to set variables on the scope $scope.foo = ResourceObject.query(); ResourceObject.query(function(results){ $scope.foo = results; //Do any other actions here…. });
  • 9. ERROR HANDLING – SECOND CALLBACK  http://stackoverflow.com/questions/20584367/how-to-handle- resource-service-errors-in-angularjs Resource.query(function(data) { // success handler }, function(error) { // error handler });
  • 10. CREATING A LAYER OF ABSTRACTION  Isolate - REST APIs may be out of your control (e.g. 3rd party). Create a factory with your own API  Simplify/Transform data Promises can be chained together The return result from one promise goes into the next as the incoming data
  • 11. EXAMPLE angular.module('sampleApp').factory('resourceAPI', ['$resource', function ($resource) { var ResourceObject = $resource("http://foo/Object/…"); return { getTransformedValues: function () { return ResourceObject.query().$promise.then(function(data) { //Make whatever transforms are required //Underscore library is useful here data = .... //The returned data will be the result //when the promise resolves return data; }); }, otherFunctionsInAPI: function() {} }; } ]);
  • 12. EXAMPLE PART II Meanwhile.... In your controller $scope.showSpinner = true; resourceAPI.getTransformedValues().then(function(data){ $scope.foo = data; return data; }, function(error){ //Perform whatever error handling you wish })[‘finally’](function(){ //Odd finally syntax is for IE8 compat $scope.showSpinner = false; });
  • 13. FUTURE STUFF TO INVESTIGATE/LEARN  http interceptors Retry Logic Security Scenarios  Response caching