unsavedChildren.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = unsavedChildren;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _ParseFile = _interopRequireDefault(require("./ParseFile"));
  8. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  9. function _interopRequireDefault(e) {
  10. return e && e.__esModule ? e : {
  11. default: e
  12. };
  13. }
  14. /**
  15. * Return an array of unsaved children, which are either Parse Objects or Files.
  16. * If it encounters any dirty Objects without Ids, it will throw an exception.
  17. *
  18. * @param {Parse.Object} obj
  19. * @param {boolean} allowDeepUnsaved
  20. * @returns {Array}
  21. */
  22. function unsavedChildren(obj, allowDeepUnsaved) {
  23. const encountered = {
  24. objects: {},
  25. files: []
  26. };
  27. const identifier = obj.className + ':' + obj._getId();
  28. encountered.objects[identifier] = obj.dirty() ? obj : true;
  29. const attributes = obj.attributes;
  30. for (const attr in attributes) {
  31. if (typeof attributes[attr] === 'object') {
  32. traverse(attributes[attr], encountered, false, !!allowDeepUnsaved);
  33. }
  34. }
  35. const unsaved = [];
  36. for (const id in encountered.objects) {
  37. if (id !== identifier && encountered.objects[id] !== true) {
  38. unsaved.push(encountered.objects[id]);
  39. }
  40. }
  41. return unsaved.concat(encountered.files);
  42. }
  43. function traverse(obj, encountered, shouldThrow, allowDeepUnsaved) {
  44. const ParseObject = _CoreManager.default.getParseObject();
  45. if (obj instanceof ParseObject) {
  46. if (!obj.id && shouldThrow) {
  47. throw new Error('Cannot create a pointer to an unsaved Object.');
  48. }
  49. const identifier = obj.className + ':' + obj._getId();
  50. if (!encountered.objects[identifier]) {
  51. encountered.objects[identifier] = obj.dirty() ? obj : true;
  52. const attributes = obj.attributes;
  53. for (const attr in attributes) {
  54. if (typeof attributes[attr] === 'object') {
  55. traverse(attributes[attr], encountered, !allowDeepUnsaved, allowDeepUnsaved);
  56. }
  57. }
  58. }
  59. return;
  60. }
  61. if (obj instanceof _ParseFile.default) {
  62. if (!obj.url() && encountered.files.indexOf(obj) < 0) {
  63. encountered.files.push(obj);
  64. }
  65. return;
  66. }
  67. if (obj instanceof _ParseRelation.default) {
  68. return;
  69. }
  70. if (Array.isArray(obj)) {
  71. obj.forEach(el => {
  72. if (typeof el === 'object') {
  73. traverse(el, encountered, shouldThrow, allowDeepUnsaved);
  74. }
  75. });
  76. }
  77. for (const k in obj) {
  78. if (typeof obj[k] === 'object') {
  79. traverse(obj[k], encountered, shouldThrow, allowDeepUnsaved);
  80. }
  81. }
  82. }