$Resource – A Time to REST

The AngularJS $resource module is a factory which creates a resource that lets you interact with RESTful server-side data sources. Basically, you configure where you’re going and what it expects and AngularJS handles everything else.

The resource “class” object has methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:

Action Verb
get GET
save POST
query GET
delete DELETE

Easily get your list by invoking the .query function

var User = $resource('/api/user', null);
var users = User.query();

What about PUT support? By default the .save will PUT your updates to the data source, but it can be easily implemented for clarity by creating a custom function.

var app = angular.module('app'. ['ngResource']);

app.factory('User',['$resource', function ($resource) {
    return $resource('/api/user/:id', null,
            'update': { method:'PUT' }

Now from your controller you simply need to call the User factory’s new update method.

User.update({id: userId}, user);

This is nice; but, what’s really cool is how AngularJS handles the return value. Unlike a Deferred / Promise value, the $resource module returns Array and Object references. At first, these objects are empty. But, when the server returns with data, AngularJS subsequently “hydrates” these empty objects with the deserialized data. This allows the $resource response to be injected into the Controller’s $scope before the server has responded.

Example User application

var app = angular.module('app' ['ngResource']);

app.factory('User',['$resource', function ($resource) {
    return $resource('/api/user/:id', null,
            'update': { method:'PUT' }

// We can retrieve a collection from the server
var users = User.query();

// create a new user
var newUser = new User();
newUser.Name = "Harry Reid";

// get a user from the server
var user = User.get({id: 1});
// update the user
user.Name = "John Smith";

// delete a user
User.remove({id: 1});

Posted in AngularJS Tagged with:

AngularJS – Getting Started

I have started to work with AngularJS over the past few weeks. What started as a weekend study project turned into a new passion. I have been doing ASP.NET MVC since it has been released and really fell in love very quickly. The loss of the bloated view state object and post backs hasn’t caused me to loose any sleep. However, with MVC there is still a lot going on, over the releases it has gotten better and I really do enjoy working with it. The WebAPI is clearly a big bonus; now my applications can make some AJAX calls from the client straight to the backend without having to go to server over a page event.

Getting Started

At my first glance, I thought WHOA! there sure is a lot to this AngularJS framework; I mean all of these different modules to learn: scope, services, controllers, providers, directives and so much more. This is so complex and going to be so hard to learn. I started out with a few introductory videos at CodeSchool.com. There are five lessons in this free course. In addition, I have done a lot of reading at the AngularJS site and I have been following Dan Wahlin.

Really after a day of looking at all of what AngularJS has to offer, it is not that hard to grasp. I was able to quickly add some Angular to an existing application and be up and running in a matter of minutes. I have not yet completely converted the application to an angular app, but I am able to add one section at a time and release it without any loss of service to the users due to the modular design of AngularJS.

What’s To Come

I am going to share my experiences with the conversion of my application to an AngularJS application over the next few weeks. I doubt that I will use all of the providers and services that AngularJS has to offer, but share what I do use.

Posted in AngularJS Tagged with: , ,

Welcome to Azure

I have decided to create a blog. I have done quite a bit of research on the idea, I knew I wanted to use WordPress, but wasn’t sure where to host it. I wanted to use Azure, but I wasn’t sure on the pricing, so I asked a friend, he said it wasn’t bad. So here I am.

Getting Started

I started this morning by creating an account and viola! So I thought. I went to the Azure Portal found WordPress, and Boom; HTTP 520 error. What the heck, in all of my years I have seen a lot of HTTP errors but never 520. Azure says things are slow but looking around it is defined as “not specified”.


Moving on

So I went to the Azure site to create it, now the list of Regions is NULL and I can not continue because Region is required and without having anything to choose from, I am stuck. I close out and try again, failed yet again. Am I ever going to get past this? Finally on my next attempt, I get a region and finally make it to where I am now.

What to expect

I have been developing software for over twenty years, during that time I have seen a lot and worked with a lot of great developers; I have mentored others, helped out creating a very successful .NET user group and a lot more. Over the years, I have taken to liking data and presenting it to users. I will be generally writing about data collection, storage and presentation.

Hope to hear lots of comments on my writings.

Posted in Azure Tagged with: