Angular.copy of Parse JS SDK object with DisableSingleInstance mode returns an empty object

Not sure if his mistake or misunderstanding of how things should work:

  • disable single instance mode
  • Create a new parsing object (or request it from the server) and fill in the properties
  • Make a copy of the object (for example, if you edited the properties on the form and wanted to save the changes / discard the changes)
  • The copy is empty ....

using parse-js-sdk 1.9.2 and angular 1.4.8

  //comment out the following line and it works ok
  Parse.Object.disableSingleInstance();

  var parseObjClass = Parse.Object.extend('Comments', {
            // Instance methods
            initialize: function (attrs, options) {}
        },
        {});

        ['name', 'desc'].forEach(function (item) {

        Object.defineProperty(parseObjClass.prototype, item, {
            get: function () {
                return this.get(item);
            },
            set: function (aValue) {
                return this.set(item, aValue);
            }
        });
    });

  var parseObj = new parseObjClass();
    parseObj.name = 'Hello World'
    parseObj.desc = 'Test Object'

  console.log('original is ' + JSON.stringify(parseObj)); 
  //--> original is {"name":"Hello World","desc":"Test Object"}

  var objCopy = angular.copy(parseObj);

  console.log('copy is ' + JSON.stringify(objCopy));
  //--> copy is {}

Here is the plunker illustrating https://plnkr.co/edit/UjWe9dl6D6Qkx9ihBSMC

background: disableSingleInstance, -, ( ) js-sdk, ...

+4
1

angular.copy(source, [destination]); docs:

. ( source, destination) .

MDN docs Object.defineProperty(obj, prop, descriptor):

, , . enumerable: true descriptor.

:

Object.defineProperty(parseObjClass.prototype, item, {
    enumerable: true,
    get: function () {
        return this.get(item);
    },
    set: function (aValue) {
        return this.set(item, aValue);
    }
});

UPDATE

, setter getter , , . get , .

Object.defineProperty(parseObjClass.prototype, item, {
    enumerable: true,
    writable: true
});

get set, - , get: () => _props[item];

+3

Source: https://habr.com/ru/post/1675496/


All Articles