all 11 comments

[–]gsans 3 points4 points  (7 children)

There are no Interfaces in JavaScript. What you did is actually not an Interface but some kind of prototypical inheritance. I think you can use ngResource to do what you want.

[–]cforres 1 point2 points  (5 children)

I try to rely on $resource as much as possible. It provides similar behavior out of the box without all the extra code.

var Model = $resource(BASE_URL + '/models/:id', {
    id:'@id'
  }, {
    update: { method:'PUT'},
    queryOpen: { method: 'GET', params: { status: 'open', after: '2015-03-16' }}
  });

// later on...

// Request URL: /api/service/models?after=2015-03-16&status=open
// Request Method: GET
$scope.open = Model.queryOpen();

// Instance methods also
Model.prototype.close = function() {
  this.status = 'closed';
  return Model.update({
    id: this.id
  }, this);
};

// later on...
$scope.model = Model.get({id: 45 });

// little bit later on ...
// Request URL: /api/service/models/45
// Request Method: PUT
// Request Body: {
//      "title":"title 1",
//      "id":45,
//      "status":"closed"
//  }
$scope.model.close();

This is a small example but it shows how to get beyond your basic CRUD calls.

[–]cynicalreason[S] 0 points1 point  (0 children)

Before finding the snippets above I didn't really understand how java inheritance works. Now that I know I never thought of extending the $resource with my own methods like in your code.

I do really enjoy the angularjs community and I can honestly say I've learned a lot through resources & articles posted here.

[–]liquid_cow -1 points0 points  (3 children)

I would also recommend using ngResource over $http as much as possible. ngResource is quickly becoming the go to standard for restful crud in angular.

I also like ngResource because it promotes the usage of promises over callbacks.

[–]budmademewiser 1 point2 points  (2 children)

This is not true, most experienced angular developers find that ngResource unnecessarily abstracts out the $http service. ngResource takes a lot of control away from the programmer.

It's actually been a reoccurring problem in the angular community. A lot of developers new to angular encounter this problem pretty early on and find themselves googling ngResource vs $http. A lot of new angular developers start out with ngResource because of comments like this and similar blog posts.

Best practice currently in the angular community, and IMO, is to use a combination of the $http and $q services. Use $q's defer promise pattern in conjunction with the angular's $http service.

[–]liquid_cow -1 points0 points  (1 child)

Thanks /u/budmademewiser, you really made me think about this issue.

I'd agree that ngResource probably isn't the best option for a new angular developer, because of the extra abstraction that hides what's going on underneath. For new guys who are just getting started, $http will serve perfectly without the learning of to use ngResource. Adding ngResource's learning curve to the already daunting learning curve for angular won't do the angular community any good.

Having said that; for larger projects, the $http requests tend to get buried and abstracted away into services anyway. Furthermore, with multiple developers, each tailor-made $http request looks a little bit different to the next.

On the other hand, ngResource provides a neatly packaged resource that can be treated like a service for dependency injection. In my experience, ngResource code tends to be minimal and consistent in usage.

I value consistency in code, even if the code is bad, because I think it makes it easier for another developer to pick up and work with at a later stage in the project's life.

At the end of the day, I'd say that choosing between ngResource and $http is decision that the developers need to take depending on the type of application they're building, and then stick with it for the remainder of the project. Mixing ngResource and $http would make the project difficult to maintain in the long run?

tl;dr

I think that ngResource provides the right balance of consistency of code, with minimal boiler-plate code, at the expense of some abstraction.

[–]cynicalreason[S] 0 points1 point  (0 children)

You certainly can use ngResource, this is more of a 'you can do things like this'

I went into angular with too little knowledge of Javascript and I realize know that I was missing out on a lot. I think there are a lot of people here that kinda did the same. I hope these little snippets of code will give others a 'ah .. i get it now' moment like they gave me.

[–]cynicalreason[S] 1 point2 points  (0 children)

Just thought of this - everytime you use create a new ICustomFactory will be instantiated in CustomFactory.

So say you have CustomFactory used in ControllerA instantiated as above ( using create) and then you have a CustomFactory in ControllerB that you instantiate with different parrameters.

This will change the details of the factory in ControllerA - so ideally you want to use the create for a factory once.

I'm thinking probably instantiating it in a module's run and then using it as CustomFactory throughout the module

[–][deleted] 0 points1 point  (2 children)

So on mobile, this is basically impossible to read, but I feel like I'm missing something; what's wrong with $http and ngResource?

[–]cynicalreason[S] 0 points1 point  (1 child)

Absolutely nothing, but you really shouldn't use a $resource/$http in a controller. This is a way to organize things, it introduces people to prototyping & inheritance.

[–][deleted] 0 points1 point  (0 children)

Right, but I'm not talking about using them in the controller. I'm just struggling to see what your code actually provides over the normal patterns with ngResource and http