SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Similar a 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
 

Similar a 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
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+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
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
+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
 

Último (20)

WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+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...
 
%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 Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
+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...
 

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