background preloader

Services/Factories

Facebook Twitter

Services.pdf - Microsoft Word Web App. Using Angular.js Factories to Get Remote Data. In this post, I’d like to show the differences between using an Angular.js factory to get local JSON data vs remote data via an AJAX call.

Using Angular.js Factories to Get Remote Data

Angular service or factory? Tl;dr is at the end In various AngularJS tutorials and documentation, the authors choose to use service or factory but don't explain why you would use one or the other.

Angular service or factory?

Few mention that value and constant are also options. Let's see why you would use one over the other. We should also understand how providers work: provider Here's the source for the provider method: Using an AngularJS Factory to Interact with a RESTful Service - Dan Wahlin. What’s covered in this Post?

Using an AngularJS Factory to Interact with a RESTful Service - Dan Wahlin

Creating a RESTful Service Creating an AngularJS Module Creating a Factory Creating a Controller AngularJS provides a great framework for building robust Single Page Applications (SPAs) and provides built-in support for routing, MV*-style programming, services and factories, modules, testing, and much more. Although Angular can consume virtually any HTTP resource, in this post I’m going to focus on using it to consume a RESTful API created using ASP.NET Web API (note that any back-end service could be used).

If you’ve worked with jQuery before then you’re used to calls such as $.getJSON() or $.ajax(). Understanding service types. Angular comes with different types of services.

understanding service types

Each one with its own use cases. Something important that you have to keep in mind is that the services are always singleton, it doesn’t matter which type you use. This is the desired behavior. NOTE: A singleton is a design pattern that restricts the instantiation of a class to just one object. Every place where we inject our service, will use the same instance. Let’s start with my own opinion about services. Javier Lerones: Preloading data using deferred promises in Angular.js. I’ve been using Angular.js for a while now and I can say that I absolutely love it.

Javier Lerones: Preloading data using deferred promises in Angular.js

The learning curve is specially steep if you are coming from jQuery-only front end development but once you get the hang of it, it is a fantastic MVW framework. I am however, trying to find the best way to pre-load data into a controller by using deferred promises inside a factory. Let me first explain why. In the app that I am building I need to access an asynchronous source of data that reads from local storage in chrome. The storage adapter I am using will trigger a callback once the data has been loaded from the disk, however the way I have designed the app, and the fact that I am using ui-router to create states instead of using Angular’s routeProvider, forces me to load all the data that the user is going to need before I present anything on the application. Angular service or factory? Custom Service - Counter.

Factory vs Service. Factory - Pushing search form data to another controller. Edit this Fiddle. Promises and Deferred objects in jQuery and AngularJS. Series of articles about futures/promises without JavaScript would not be complete.

Promises and Deferred objects in jQuery and AngularJS

Futures (more commonly named promises in JS land) are ubiquitous in JavaScript to the point where we almost don't recognize them any more. AJAX, timeouts and whole Node.JS are built on top of asynchronous callbacks. Nested callbacks (as we will see in just a second) are so hard to follow and maintain that the callback hell term was coined.

In this article I will explain how promises can improve readability and modularize your code. Introducing promise object Let's take the first, simplest example using AJAX and $.getJSON() helper method: $.getJSON('square/3', function(square) { console.info(square); }); square/3 is an AJAX resource that yields 9 (3 square). Suddenly business logic is buried deeply inside nested callbacks (as a matter of fact this is still not bad, but it tends to be much worse in practice). What does square() or more precisely $.getJSON() return? So, what's the difference? Or: in jQuery. Post: Angular JS, Lift 3, and Streaming Promises. Simple AngularJS Lift has always had the best server-push technology around.

Post: Angular JS, Lift 3, and Streaming Promises

Why? It's secure, it deals well with spotty connections, it respects the limited number of HTTP connections between the client and the server, and so much more. Angular JS is a very exciting UI package that makes building dynamic single-page applications a snap because there's a 2-way binding between the model and the UI so that changes in the model are correctly reflected in the UI. And the whole binding is declarative so that once you use a model item in the UI, that part of the UI is always updated when the model changes.

Round Trips The default way to build Angular JS apps is to associate certain events in the client with REST calls to the server and when the REST calls are satisfied, the model is updated and UI is redrawn. But there are issues: Lift's round trips have the following advantages over simple REST: Simple Round Trip. $http/promise. AngularJS. Models and Services in Angular. Image from.

Models and Services in Angular

Pjax and AngularJS. What is the difference between module.factory and module.service and how might both be applied? Lazy-loaded asynchronous resources. Service - applied to scope.