vendredi 26 décembre 2014

Subclass Parse.Object in Javascript


Forgive me if this is a stupid question, I've been doing iphone and android for a while now and recently I need to develop for the web. I'm using parse.com to handle my server requests. According to their documentation, I can do a subclass like this.



//A complex subclass of Parse.Object
var Monster = Parse.Object.extend("Monster", {
// Instance methods
hasSuperHumanStrength: function () {
return this.get("strength") > 18;
},
// Instance properties go in an initialize method
initialize: function (attrs, options) {
this.sound = "Rawr"
}
}, {
// Class methods
spawn: function(strength) {
var monster = new Monster();
monster.set("strength", strength);
return monster;
}
});

var monster = Monster.spawn(200);
alert(monster.get('strength')); // Displays 200.
alert(monster.sound); // Displays Rawr.


Ultimately I'm trying to translate the following code from Java to JS.



/**
* @author XujieSong
*
*/
@ParseClassName("_User")
public class SHUser extends ParseUser {

/**
* SHUser is a subclass of ParseUser
* Class name _User
*/

/**
* Default constructor
*/
public SHUser() {

}

/**
* Create a SHUser object with known objectId
* This method only returns a SHUser without data
* @param userID the objectId of the SHUser
* @return user a reference to a SHUser
*/
public SHUser(String userId) {
this.setObjectId(userId);
}

/**
* Create a new SHUser with attributes
* @param userName
* @param password
* @param email
* @param displayName
* @param installation
* @param profileImage
* @return user a new user
*/
public SHUser(String userName, String password, String email, String displayName) {
this.setUsername(userName);
this.setPassword(password);
this.setEmail(email);
this.setDisplayName(displayName);
}
}


And this is what I have got so far,



var SHUser = Parse.Object.extend("_User", {
/**
* Instance properties go in an initialize method
* @param {userId}
* @return {[type]}
*/
SHUser: function () {

},
/**
* Instance properties go in an initialize method
* @param {userId}
* @return {[type]}
*/
SHUser: function (userId) {
this.id = userId;
},
/**
* Instance properties go in an initialize method
* @param {userName}
* @param {password}
* @param {email}
* @param {displayName}
* @return {[type]}
*/
SHUser: function (userName, password, email, displayName) {
this.setUsername(userName);
this.setPassword(password);
this.setEmail(email);
this.setDisplayName(displayName);
}
}, {
// Class methods
});


after



var user = new SHUser(userId);
window.alert(Shelf.seller.id);


I got undefined.


So here's the question. Is it possible to have a default constructor, then a few customized constructors like the way it is in Java? Is it a good practice to do so? Thank you.





Aucun commentaire:

Enregistrer un commentaire