Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nanlabsweb
Created August 18, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanlabsweb/2506bd17deda9e294c9221f84076d803 to your computer and use it in GitHub Desktop.
Save nanlabsweb/2506bd17deda9e294c9221f84076d803 to your computer and use it in GitHub Desktop.
POST - JS plain object example.
/**
* contact.js
**/
var Contact = function () {
// Constructor, sets the defaults values of the object
var clazz = function (attributes) {
var defaults = {
id: null,
contactType: CONTACT_TYPES.UNKNOWN,
firstName: null,
lastName: null,
phone: null,
address: null,
colleague: {
from: null,
internPhone: null,
isCurrent: null
}
};
_.extend(this, defaults, attributes);
};
// Class Methods
_.extend(clazz.prototype, {
isNew: function () {
return (this.id === null);
},
isFriendContactType: function () {
return (this.contactType === CONTACT_TYPES.FRIEND);
},
isFamilyContactType: function () {
return (this.contactType === CONTACT_TYPES.FAMILY);
},
isColleagueContactType: function () {
return (this.contactType === CONTACT_TYPES.COLLEAGUE);
},
isUnknownContactType: function () {
return (this.contactType === CONTACT_TYPES.UNKNOWN);
},
getId: function() {
return this.id.toString();
},
getFullName: function() {
if (this.firstName !== '' && this.lastName !== '') {
return this.firstName + ', ' + this.lastName;
} else if (this.firstName !== '') {
return this.firstName;
} else if (this.lastName !== '') {
return this.lastName;
} else {
return '';
}
},
// Validation helper methods
isPhoneRequired: function() {
return (!this.isUnknownContactType());
},
isAddressRequired: function() {
return (!this.isUnknownContactType());
},
/**
* Useful method to parse server response and populate the fields.
*/
populateData: function (data) {
_.extend(this, data);
}
});
return clazz;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment