【发布时间】:2015-05-25 01:30:03
【问题描述】:
我正在使用restangular,到目前为止,一切都很好,但是,我有这个问题无法解决。
我用这样的基本操作定义了一个抽象存储库:
app.factory('AbstractRepository', [
function(){
function AbstractRepository(restangular, route) {
this.restangular = restangular;
this.route = route;
};
AbstractRepository.prototype = {
getList: function (params) {
return this.restangular.all(this.route).getList(params).$object;
},
get: function (id) {
return this.restangular.one(this.route, id).get();
},
getView: function (id) {
return this.restangular.one(this.route, id).one(this.route + 'view').get();
},
update: function (updatedResource) {
return updatedResource.put().$object;
},
create: function (newResource) {
return this.restangular.all(this.route).post(newResource);
},
remove: function (object) {
return this.restangular.one(this.route, object.id).remove();
},
};
AbstractRepository.extend = function (repository) {
repository.prototype = Object.create(AbstractRepository.prototype);
repository.prototype.constructor = repository;
}
return AbstractRepository;
}
]);
以及具体的仓库:
app.factory('ServiceRepository', ['Restangular', 'AbstractRepository',
function (restangular, AbstractRepository) {
function ServiceRepository() {
//restangular.setBaseUrl("http://192.168.0.144:8080/api/rest/services/");
AbstractRepository.call(this, restangular,'http://192.168.0.144:8080/api/rest/services/');
}
AbstractRepository.extend(ServiceRepository);
return new ServiceRepository();
}
我调用方法:
ServiceRepository.getList();
现在我想实现和功能(getServicesByOperatorId),它只适用于特定的存储库,而不是抽象的。所以我可以这样称呼它:
ServiceRepository.getServicesByOperatorId({"operatorId":7});
如果我在抽象的原型中定义函数,它可以工作,但我希望我在具体的原型中定义。
非常感谢您的宝贵时间。
【问题讨论】:
-
您应该扩展 ServiceRepository,而不是 Abstract 的。您必须在子节点而不是父节点提供扩展
-
我该怎么做?我尝试:
ServiceRepository.prototype = Object.create(AbstractRepository.prototype, { getServicesByOperatorId : function (id) { return this.restangular.all(this.route + 'getServiceByOperatorId').getList(id).$object; } }); ServiceRepository.prototype.constructor = ServiceRepository;我可以访问 getList() 函数,但不能访问 getServicesByOperatorId({"operatorId":7});
标签: angularjs rest restangular