[英]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. 在AngularJS中有以下“类”,我似乎无法重载构造函数方法,我需要这样做,我也可以用我的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. Angular的$injector
负责创建/实例化您的服务和工厂,因此您不能直接要求构造函数args。 However, if you have one that requires custom input, you can approach it a few ways: 但是,如果您有一个需要自定义输入的,您可以通过以下几种方式处理它:
CharacterBuilder.create(params)
). 创建一个公开实例化类的工厂方法的服务(例如CharacterBuilder.create(params)
)。 init
method to your service that acts as the constructor normally would (eg Character.init(params)
). 向您的服务添加一个init
方法,该方法通常用作构造函数(例如Character.init(params)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.