简体   繁体   中英

Overloading the constructor

Having the following 'class' in AngularJS , i cannot seem to overload the constructor method, which i would need to do so i can also construct it with data from my API.

How would i go about and do this properly?

Character class

angular.module('sheetApp')
    .factory('Character', function ($rootScope, $resource, $state, ApiLocation) {

        function Character() {
            this.name = "";
            this.level = 1;
            this.health = 1;
            this.mana = 1;
            this.race = null;
            this.guild = null;
            this.colors = [];
            this.attributes = {Strength: 1, Intelligence: 1, Dexterity: 1, Endurance: 1, Charisma: 1};
            this.passives = [];
            this.skills = [];
            this.spells = [];
            this.equipment = [];
            this.consumables = [];
            this.user = "";

            this.maxPassives = 4;
            this.maxSpells = 4;

            angular.forEach($rootScope.skills, function (skill) {
                    this.push({skill: skill, points: [false, false, false, false, false], level: 0});
                },
                this.skills);
        }


        Character.prototype = {

            setRace: function (race) {
                this.race = race;

                $rootScope.notification = {
                    type: "success",
                    text: "Added race " + race.name + " to character.",
                    show: true,
                    timer: 5000
                };
                $state.go('sheet');
            },

            setGuild: function (guild) {
                this.guild = guild;

                $rootScope.notification = {
                    type: "success",
                    text: "Added guild " + guild.name + " to character.",
                    show: true,
                    timer: 5000
                };
                $state.go('sheet');
            },

            // Add passive to character, checks against duplicates.
            addPassive: function (passive) {
                var duplicate = false;

                angular.forEach(this.passives, function (pas) {
                    if (passive.name === pas.name) {
                        duplicate = true;
                        $rootScope.notification = {
                            type: "danger",
                            text: "Character already has " + passive.name,
                            show: true,
                            timer: 5000
                        };
                    }
                });

                if (!duplicate) {
                    if (this.passives.length < this.maxPassives) {
                        this.passives.push(passive);
                        $rootScope.notification = {
                            type: "success",
                            text: "Added passive " + passive.name + " to character.",
                            show: true,
                            timer: 5000
                        };
                        $state.go('sheet');
                    }
                    else {
                        $rootScope.notification = {
                            type: "danger",
                            text: "You already have " + this.maxPassives +  " passives",
                            show: true,
                            timer: 5000
                        };
                        $state.go('sheet');
                    }
                }
            },

            // Add spell to character, checks against duplicates.
            addSpell: function (spell) {
                var duplicate = false;

                angular.forEach(this.spells, function (sp) {
                    if (spell.name === sp.name) {
                        duplicate = true;
                        $rootScope.notification = {
                            type: "danger",
                            text: "Character already has " + spell.name,
                            show: true,
                            timer: 5000
                        };
                    }
                });

                if (!duplicate) {
                    this.spells.push(spell);

                    $rootScope.notification = {
                        type: "success",
                        text: "Added spell " + spell.name + " to character.",
                        show: true,
                        timer: 5000
                    };
                    $state.go('sheet');
                }
            },

            resetSkillPoints: function () {
                angular.forEach(this.skills, function (skill) {
                    for (var i = 0; i <= 4; i++) {
                        if (skill.points[i] == true) {
                            skill.level += 1;
                        }
                        skill.points[i] = false;
                    }
                });
            }
        };

        Character.resource =
            $resource(ApiLocation + '/characters/:link', {}, {
                getUser: {method: 'GET', params: {link: 'user', name: '@name'}, isArray: true},
                getChar: {method: 'GET', params: {link: 'character', name: '@name'}},
                save: {method: 'POST'},
                query: {method: 'GET', isArray: true, cache: true},
                remove: {method: 'DELETE'},
                delete: {method: 'DELETE'}
            });


        return ( Character );
    });

Angular's $injector is responsible for creating/instantiating your services and factories, so you can't directly require constructor args. However, if you have one that requires custom input, you can approach it a few ways:

  1. Create a service that exposes a factory method that instantiates your class (eg CharacterBuilder.create(params) ).
  2. Add an init method to your service that acts as the constructor normally would (eg Character.init(params) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM