Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import { AbilityScore } from 'charactersheet/models/character/ability_score';
  2. import {
  3. CoreManager,
  4. Notifications
  5. } from 'charactersheet/utilities';
  6. import ko from 'knockout';
  7.  
  8. export class AbilityScoresViewModelDelegate {
  9. constructor() {}
  10. /**
  11. * This method takes care of persisting the updated ability scores and sending out relevant
  12. * notifications.
  13. *
  14. * @param {AbilityScore[]} oldScores initial ability scores
  15. * @param {AbilityScore[]} newScores updated ability scores
  16. * @returns {AbilityScore[]} an array of the most up to date ability scores
  17. */
  18. updateAbilityScores = async (oldScores, newScores) => {
  19. let scoresModified = false;
  20. oldScores().forEach(async (old, i, _) => {
  21. let newScore = newScores().filter((element) => {
  22. return old.shortName() === element.shortName();
  23. });
  24.  
  25. if (newScore.value() != old.value()) {
  26. scoresModified = true;
  27. const response = await newScore().ps.save();
  28. self.sendNotifications(response.object);
  29. }
  30. });
  31.  
  32. if (scoresModified) {
  33. Notifications.abilityScores.changed.dispatch();
  34. var key = CoreManager.activeCore().uuid();
  35. const response = await AbilityScore.ps.list({coreUuid: key});
  36. return response.objects;
  37. } else {
  38. return oldScores;
  39. }
  40. };
  41.  
  42. sendNotifications = (score) => {
  43. if (score.shortName() === 'INT') {
  44. Notifications.abilityScores.intelligence.changed.dispatch();
  45. } else if (score.shortName() === 'DEX') {
  46. Notifications.abilityScores.dexterity.changed.dispatch();
  47. }
  48. };
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement