Search code examples
javascriptjavaandroidparse-platform

JavaScript subclassing in Parse.com


In Android I subclassed ParseObject with two local variables that are not in Parse class. I just needed to set those variables locally and had no need to save them on server. They are String's named helper1 and helper2 with getters and setters as well.

It works all fine on Android - I can use setHelper1("whatever"); as well as getHelper() methods on my ParseObject's.

What I want to do is to do the same in JavaScript since I want to make same operation in ParseCloud and make it return results with that additional Strings without creating additional columns in database class.

I read https://parse.com/docs/js/guide#objects-parse-object and https://parse.com/docs/js/guide#queries-basic-queries but it's not helping very much and I can't get it. How that could be achieved?

edit:

q1.find({
    success: function(results){     

    for (var x in results){
        x.helper1 = 'foo';
    }

     response.success(results);
    },
    error: function(error){

    }
});

Solution

  • As far as I know, those parse-objects are stored in the form of a JSON object.

    In JavaScript, If you'd like to add an additional property (playing role of a class member) to an existing object, it's enough to use this code.

    var myobj = /* This is that parse-object */
    
    // Add property helper1
    myobj.helper1 = 'foo';
    
    // Add property helper2
    myobj.helper2 = 'bar';
    

    For removing those properties, use this code.

    // Remove property helper1
    delete myobj.helper1;
    
    // Remove property helper2
    delete myobj.helper2;
    

    Equivalently, you can use [] to create and access a property.

    // Add property help1
    myobj['helper1'] = 'foo';
    
    // Access it
    console.log(myobj['helper1']);