项目作者: aj-dev

项目描述 :
An AngularJS factory for simple class based inheritance with support for mixins
高级语言: JavaScript
项目地址: git://github.com/aj-dev/angular-base-class.git
创建时间: 2015-02-07T16:17:35Z
项目社区:https://github.com/aj-dev/angular-base-class

开源协议:MIT License

下载


angular-base-class

Build Status Dependencies DevDependencies Coverage Status npm

An AngularJS factory for simple class based inheritance with support for mixins

Table of contents

  1. Key features
  2. Installation
  3. Usage
  4. Running tests
  5. Credits
  6. License

Key features

  • Constructor definition is optional
  • Uses this._super('methodName', arguments) to call super class methods
  • Supports multiple inheritance by resolving correct this context in super() calls
  • Mixins can be injected and used by adding them to mixins: []
  • Does not override inherited mixins
  • Compatible with AngularJS 1.2.x - 1.6.x

Installation

  • npm
    • npm i angular-base-class --save
    • include a reference to node_modules/angular-base-class/angular-base-class.js
  • bower
    • bower install angular-base-class --save
    • include a reference to bower_components/angular-base-class/angular-base-class.js

Usage

  • Include angular-base-class.min.js in your project
  • Add module BaseClass as dependency to your AngularJS app
Default constructor
  1. angular.module('App', ['BaseClass'])
  2. .factory('Mammal', ['BaseClass', function (BaseClass) {
  3. return BaseClass.extend({
  4. setAge: function (age) {
  5. this.age = age;
  6. },
  7. getAge: function () {
  8. return this.age;
  9. }
  10. });
  11. }])
  12. .factory('Dog', ['Mammal', function (Mammal) {
  13. return Mammal.extend({
  14. setAge: function (age) {
  15. this._super('setAge', age + 10);
  16. },
  17. getAge: function () {
  18. return 'Dog is ' + this._super('getAge') + ' years old';
  19. }
  20. })
  21. }])
  22. .controller('Ctrl', ['$scope', 'Dog', function ($scope, Dog) {
  23. $scope.dog = new Dog({age: 5});
  24. $scope.dog.getAge(); // Dog is 5 years old
  25. $scope.dog.setAge(8);
  26. $scope.dog.getAge(); // Dog is 18 years old
  27. }]);
Custom constructor
  1. angular.module('App', ['BaseClass'])
  2. .factory('Mammal', ['BaseClass', function (BaseClass) {
  3. return BaseClass.extend({
  4. constructor: function (args) {
  5. this.age = args.age + 1;
  6. },
  7. setAge: function (age) {
  8. this.age = age;
  9. },
  10. getAge: function () {
  11. return this.age;
  12. }
  13. });
  14. }])
  15. .factory('Dog', ['Mammal', function (Mammal) {
  16. return Mammal.extend({
  17. constructor: function () {
  18. this._super('constructor', arguments);
  19. },
  20. setAge: function (age) {
  21. this._super('setAge', age + 10);
  22. },
  23. getAge: function () {
  24. return 'Dog is ' + this._super('getAge') + ' years old';
  25. },
  26. })
  27. }])
  28. .controller('Ctrl', ['$scope', 'Dog', function ($scope, Dog) {
  29. $scope.dog = new Dog({age: 5});
  30. $scope.dog.getAge(); // Dog is 6 years old
  31. $scope.dog.setAge(8);
  32. $scope.dog.getAge(); // Dog is 18 years old
  33. }]);
With mixins
  1. angular.module('App', ['BaseClass'])
  2. .factory('mammalMixin', [function () {
  3. return {
  4. grow: function (number) {
  5. this.age += number;
  6. },
  7. getName: function () {
  8. return this.name;
  9. }
  10. };
  11. }]);
  12. .factory('Mammal', ['BaseClass', 'mammalMixin', function (BaseClass, mammalMixin) {
  13. return BaseClass.extend({
  14. constructor: function (args) {
  15. this.name = args.name;
  16. this.age = args.age;
  17. },
  18. setAge: function (age) {
  19. this.age = age;
  20. },
  21. getAge: function () {
  22. return this.age;
  23. },
  24. mixins: [mammalMixin]
  25. });
  26. }])
  27. .factory('Cat', ['Mammal', function (Mammal) {
  28. return Mammal.extend({
  29. getAgeAndName: function () {
  30. return 'Age: ' + this.getAge() + ' Name: ' + this.getName();
  31. }
  32. });
  33. }])
  34. .controller('Ctrl', ['$scope', 'Cat', function ($scope, Cat) {
  35. $scope.dog = new Cat({name: 'Meow', age: 5});
  36. $scope.dog.getAge(); // 5
  37. $scope.dog.setAge(8);
  38. $scope.dog.getAge(); // 8
  39. $scope.dog.getName(); // Meow
  40. $scope.dog.getAgeAndName(); // Age: 8 Name: Meow
  41. $scope.dog.grow(2);
  42. $scope.dog.getAge(); // 10
  43. }]);

Running tests

  • Get the source code and in project root directory:
    • npm install
    • npm run test

Credits

License

Licensed under the MIT license. Copyright (c) 2015 - 2017 Audrius Jakumavicius