【问题标题】:Restangular - Specific Function to Specific FactoryRestangular - 特定工厂的特定功能
【发布时间】: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


【解决方案1】:

我终于找到了一种方法来做我想做的事:

在特定的工厂中定义原型并从抽象原型扩展,如下所示:

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()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多