where to place resource specific logic

后端 未结 2 2163
眼角桃花
眼角桃花 2021-02-14 14:18

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 m

相关标签:
2条回答
  • 2021-02-14 14:31

    You might want to take a look at my answer to this SO question on related topic.

    With such a solution, domain specific logic goes into custom domain entity class (in particular, its prototype).

    0 讨论(0)
  • 2021-02-14 14:46

    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;    
    
    }]);
    
    0 讨论(0)
提交回复
热议问题