簡體   English   中英

重載構造函數

[英]Overloading the constructor

AngularJS中有以下“類”,我似乎無法重載構造函數方法,我需要這樣做,我也可以用我的API中的數據構造它。

我該如何正確地做到這一點?

角色類

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的$injector負責創建/實例化您的服務和工廠,因此您不能直接要求構造函數args。 但是,如果您有一個需要自定義輸入的,您可以通過以下幾種方式處理它:

  1. 創建一個公開實例化類的工廠方法的服務(例如CharacterBuilder.create(params) )。
  2. 向您的服務添加一個init方法,該方法通常用作構造函數(例如Character.init(params) )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM