Angularjs 重新启动特定工厂的特定功能
我正在与restangular合作,到目前为止,一切都很好,但是,我有一个无法解决的问题 我使用以下基本操作定义了一个抽象存储库:Angularjs 重新启动特定工厂的特定功能,angularjs,rest,restangular,Angularjs,Rest,Restangular,我正在与restangular合作,到目前为止,一切都很好,但是,我有一个无法解决的问题 我使用以下基本操作定义了一个抽象存储库: app.factory('AbstractRepository', [ function(){ function AbstractRepository(restangular, route) { this.restangular = restangular; this.route = route; };
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;
}
]);
ServiceRepository.getServicesByOperatorId({"operatorId":7});
ServiceRepository.prototype = {
getServicesByOperatorId: function (id) {
return this.restangular.all(this.route + 'getServicesByOperatorId').getList(id).$object;
}
}
angular.extend(ServiceRepository.prototype, AbstractRepository.prototype);
以及具体的回应:
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),它只在特定的存储库中工作,而不是抽象地工作。所以我可以这样称呼它:
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;
}
]);
ServiceRepository.getServicesByOperatorId({"operatorId":7});
ServiceRepository.prototype = {
getServicesByOperatorId: function (id) {
return this.restangular.all(this.route + 'getServicesByOperatorId').getList(id).$object;
}
}
angular.extend(ServiceRepository.prototype, AbstractRepository.prototype);
如果我在抽象的原型中定义函数,它就可以工作,但我想在特定的原型中定义函数
非常感谢您抽出时间。最后,我找到了一种方法来做我想做的事情: 在特定工厂中,定义原型并从抽象原型扩展如下:
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;
}
]);
ServiceRepository.getServicesByOperatorId({"operatorId":7});
ServiceRepository.prototype = {
getServicesByOperatorId: function (id) {
return this.restangular.all(this.route + 'getServicesByOperatorId').getList(id).$object;
}
}
angular.extend(ServiceRepository.prototype, AbstractRepository.prototype);
在抽象存储库中,删除以下定义:
AbstractRepository.extend = function (repository) {
repository.prototype = Object.create(AbstractRepository.prototype);
repository.prototype.constructor = repository;
}
不,我可以访问de getList()方法和getServicesByOperatorId()。您应该扩展ServiceRepository,而不是抽象的ServiceRepository。你必须向孩子而不是家长提供扩展我怎么做?我尝试:
ServiceRepository.prototype=Object.create(AbstractRepository.prototype,{getServicesByOperatorId:function(id){返回this.restangular.all(this.route+'getServiceByOperatorId').getList(id)。$Object;});ServiceRepository.prototype.constructor=ServiceRepository代码>和我可以访问getList()函数,但不能访问getServicesByOperatorId({“operatorId”:7});