在角服务扩展一个基类 [英] Extending a base class in an Angular service

查看:148
本文介绍了在角服务扩展一个基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类,我想在服务扩展到帮助的角度范围内获取数据。我已搜查四处撒网的解决方案,但还没有找到一个我喜欢的。我有用于访问设备的文件系统的基类

I have a base class that I would like to extend in a service to help get data in to the angular scope. I have searched around the net for a solution, but have not found one that I like. I have a base class that is used to access the File systems of devices

类结构:

var cOfflineStorageBase = Class.extend({
   init: function(){
   },
   CreateFolderDir: function(){
   },
   DeleteAll: function(){
   },
   DeleteDirectories: function(){
   },
   DeleteItem: function(){
   },
   GetFiles: function(){
   },
   FileExists: function(){
   }, 
   GetPath: function(){
   },
   GetObject: function(){
   },
   SaveObject: function(){
   },   
});

我想能够在几个不同的角度的服务扩展这个类(即offlineCart,offlineCustomLists,等...),其中每个服务将能够使用存储基来存储各种不同的数据类型。我找最好的,最合适的方式的角度来做到这一点。在香草JavaScript的人会只是做这样的事情:

I would like to be able to extend this class in several different angular services (ie offlineCart, offlineCustomLists, ect...) where each service would be able to use the storage base to store the various different data types. I am looking for the best, most appropriate way to do this in angular. In vanilla JavaScript one would just do something like this:

var newClass = cOfflineStorageBase.extend({
   //add new stuff here
});

但我想这样做同样的事情的角度的方式。

but I want to do this same thing the angular way.

我一直在考虑的方法是使用angular.extend功能,但我不知道这是适当的,或将这样的事情是一个更合适的方法:

The approach I have been considering are to use the angular.extend functionality, but I am not sure this is appropriate or would something like this be a more appropriate approach:

app.factory('someChild', ['$http' , 'cOfflineStorageBase', 
            function($http, cOfflineStorageBase){
  var SomeClass = cOfflineStorageBase.extend({
     init: function(){
        this._super.init()
     },
     //Add more stuff here
  });
  return SomeClass;
}]);

我想一些建议,如果论文的方法是正确的,或者有可能是另一种是因为我想完成什么好。我还想或者更确切地说,需要使用承诺在本得多code,因为它是异步。

I would like some advice if theses approaches are correct or if there might be another that is better for what I am wanting to accomplish. I would also like or rather need to use promises in much of this code as it would be async.

推荐答案

最近,我被拉断这一招。

I pulled off this trick recently.

我将定义一个简单的JavaScript构造开始。这并不需要是一个角的服务。我要做的是,后来,延伸构造可以通过参数传递任何必要的注射。因此,这将是我的角度服务基地类。这是我将暴露什么,我希望所有角度的服务来继承。

I will start by defining a plain JavaScript constructor. This does not need to be an angular service. What I do is that, later, the extending constructors can pass any necessary injections by parameter. So, this will be the base "class" of my angular services. This is where I would expose anything I want all angular services to inherit.

function ParentService($http) {
   this.$http = $http;
}

ParentService.prototype.foo = function () {
    alert("Hello World");
};

然后我将继续使用原型继承来定义一个孩子的构造。此构造的确将是一个角度的服务(您可以通过我的使用 $的告诉在年底注入)。

function ChildService($http) {
    Parent.call(this, $http);
}

ChildService.prototype = new ParentService();
ChildService.prototype.baz = function() {
   return this.$http.get('/sample/rest/call');
}
ChildService.$inject = ['$http'];

然后我将进行相应的角模块注册点菜服务:

Then I will proceed to register the services à la carte in the corresponding angular modules:

var app = angular.module('SampleApp', []);
app.service('child', ChildService);

最后,在我的控制器我会简单地注入我的服务,这将是我的ChildService构造,从而扩展了我的ParentService构造的一个实例:

Finally, in my controller I will simply inject my service, which will be an instance of my ChildService constructor, which in turn extends my ParentService constructor:

app.controller('MainCtrl', ['$scope', 'child', function ($scope, child) {
    child.foo(); //alert("Hello World")
    var promise = child.bar();
}]);

您可以看到的jsfiddle 这里

You can see a JSFiddle here

也有在YouTube上的视频有趣从ngConf称为书写一个巨大的角应用其中涉及到了一些主题和一些其他的想法与角code可重用性。

Also there is an interesting video in Youtube from ngConf called Writing A Massive Angular App which covers some of these topics and a few other ideas on code reusability with angular.

这篇关于在角服务扩展一个基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆