在哪里放置资源的特定逻辑 [英] where to place resource specific logic
本文介绍了在哪里放置资源的特定逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以帮我请考虑在何处放置资源(服务)在AngularJS具体的业务逻辑。我觉得这应该是伟大的创造超过我的一些资源模特般的抽象,但我不知道怎么样。
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调用:
> GET /customers/1
< {"first_name": "John", "last_name": "Doe", "created_at": '1342915200'}
资源(以CoffeScript):
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
推荐答案
逻辑,需要在域对象的实例要调用的最好的地方将是一个此域对象的原型。
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.
您可以写类似的规定:
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;
}]);
这篇关于在哪里放置资源的特定逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文