Parse-SDK-JS icon indicating copy to clipboard operation
Parse-SDK-JS copied to clipboard

Add dot notation to get/set `Parse.Object` attributes

Open dblythy opened this issue 4 years ago • 7 comments

Is your feature request related to a problem? Please describe. VueJS is pretty heavy on binding data using dot notation. This makes things pretty difficult when using Parse, as to get properties you need to use .get(key) and .set(key).

This means that you have to constantly convert your data to and from JSON and Parse Objects.

Describe the solution you'd like Be able to use dot notation for .get(key) and .set(key). This would mean, for example, you could bind an input to obj.toJSON().name <input v-model="obj.toJSON().name" placeholder="edit me">, without having to have a function to pass it to another JSON object.

Describe alternatives you've considered Subclassing using Proxy, but doesn't work with vueJS

Additional context

dblythy avatar Mar 05 '21 15:03 dblythy

closing for now as this is probably better suited to StackOverflow / the forum

dblythy avatar Mar 05 '21 16:03 dblythy

use getters and setters;

    constructor () {
       super ("ClassName");
    }

   get property1() {
      //feel free to do some data manipulation here if you want to before returning the data
      return this.get('property1');
   }
   set property1(value) {
      //feel free to do some data manipulation here if you want to before saving  the data
       this.set('property1', value);
   }
}

bobokeke0 avatar Oct 16 '21 07:10 bobokeke0

Thanks for opening this issue!

This would cause backwards compatibility issues if this feature is enable by default.

If some users are using the ParseObject to store extra temporary info, the get/set intercept temporary user data.

sadortun avatar Aug 11 '23 03:08 sadortun

I think this has been discussed in https://github.com/parse-community/Parse-SDK-JS/pull/1484 and been accepted as a caveat that would go into the docs. Setting a custom property on a foreign-managed object is always at risk of breaking and probably not good practice, because Parse.Object could get a new property in the future that conflicts with the custom property. And adding a new property would not be considered a breaking change.

mtrezza avatar Aug 14 '23 12:08 mtrezza

If someone else is interested, we've used decorators on custom parse objects to add get/set notation on the properties we want saved:

decorator:

export function ParseField<T>(
    target: Parse.Object,
    key: string,
): void {
    Object.defineProperty(target, key, {
        get: function () {
            return (this as Parse.Object).get(key);
        },
        set: function (value: T) {
            if (value === undefined || value === null) {
                (this as Parse.Object).unset(key);
            } else {
                (this as Parse.Object).set(key, value);
            }
        },
        enumerable: true,
        configurable: true,
    });
}

example:

export class MyObject extends Parse.Object {
    @ParseField
    public type: ProcessTypes;

    @ParseField
    public name: string;

    @ParseField
    public description: string;

    @ParseField
    public steps: Step[];
}

mortenmo avatar Sep 22 '23 21:09 mortenmo

Hello, if anyone is interested I open sourced my solution for providing types and dot notation for custom database classes. https://www.npmjs.com/package/parse-sdk-ts

theolundqvist avatar Oct 12 '23 20:10 theolundqvist