你能帮助我,考虑在AngularJS中放置资源(服务)的业务逻辑.我觉得在我的资源上创建一些类似抽象的抽象应该是很棒的,但我不知道如何.
API调用:
> GET /customers/1 < {"first_name": "John","last_name": "Doe","created_at": '1342915200'}
资源(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} }) ])
我想做一些像:
c = CustomerService.get(1) c.full_name() => "John Doe" c.months_since_creation() => '1 month'
非常感谢任何想法.
亚当
解决方法
需要在域对象实例上调用逻辑的最佳位置将是该域对象的原型.
你可以写一些这样的东西:
services.factory('CustomerService',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; }]);