JavaScript where to place resource specific logic

Can you help me please to consider where to place resource (service) specific business logic in AngularJS. I feel it should be great to create some model-like abstraction over my resource, but I'm not sure how.

API call:

> GET /customers/1
< {"first_name": "John", "last_name": "Doe", "created_at": '1342915200'}

Resource (in CoffeScript):

services = angular.module('billing.services', ['ngResource'])
services.factory('CustomerService', ['$resource', ($resource) ->
  $resource('http://virtualmaster.apiary.io/customers/:id', {}, {
    all: {method: 'GET', params: {}},
    find: {method: 'GET', params: {}, isArray: true}
  })
])

I'd like to do something like:

c = CustomerService.get(1)
c.full_name()
=> "John Doe"

c.months_since_creation()
=> '1 month'

Thanks a lot for any ideas. Adam

Answer:1

The best place for logic that needs to be invoked on an instance of a domain object would be a prototype of this domain object.

You could write something along those lines:

services.factory('CustomerService', ['$resource', function($resource) {

    var CustomerService = $resource('http://virtualmaster.apiary.io/customers/:id', {}, {
        all: {
            method: 'GET',
            params: {}
        }
        //more custom resources methods go here....
    });

    CustomerService.prototype.fullName = function(){
       return this.first_name + ' ' + this.last_name;
    };

    //more prototype methods go here....

    return CustomerService;    

}]);
Answer:2

I'm using sinon.js as a way to stub out dependencies in my Mocha tests. I prefer the 'spy' approach over a classic mock approach, as the introspection of the spy seems clearer and affords more ...

I'm using sinon.js as a way to stub out dependencies in my Mocha tests. I prefer the 'spy' approach over a classic mock approach, as the introspection of the spy seems clearer and affords more ...

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript System.InvalidOperationException: SaveBOAT Web Service method name is not valid. ...

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript System.InvalidOperationException: SaveBOAT Web Service method name is not valid. ...

  1. service method name is not valid
  2. web service method name is not valid
  3. web service method name is not valid. c#
  4. web service method name is not valid asmx
  5. web service method name is not valid vb.net
  6. web service method name is not valid json
  7. web service method name is not valid ajax
  8. web service method name is not valid. jquery asmx
  9. $metadata web service method name is not valid
  10. soap web service method name is not valid
  11. postman web service method name is not valid
  12. soaphttpclientprotocol webservice method name is not valid

I have code like the following: function test(obj) { if(//some conditon) { obj.onload(); }else{ obj.onerror(); } } for(var i=0;i<4;i++){ test({ onload:...

I have code like the following: function test(obj) { if(//some conditon) { obj.onload(); }else{ obj.onerror(); } } for(var i=0;i<4;i++){ test({ onload:...

In Three.js, I would like to use THREE.quaternion to make the camera object rotate to the selected object. I did search the web but found no example/demo or document about how to use this quaternion ...

In Three.js, I would like to use THREE.quaternion to make the camera object rotate to the selected object. I did search the web but found no example/demo or document about how to use this quaternion ...

  1. three.js rotate camera quaternion
  2. three js camera quaternion
  3. three.js rotate object quaternion