1 /** 2 * @fileOverview JavaScript representation of the Finesse Contact object. 3 * 4 * @name finesse.restservices.Contact 5 * @requires finesse.clientservices.ClientServices 6 * @requires Class 7 * @requires finesse.FinesseBase 8 * @requires finesse.restservices.RestBase 9 */ 10 11 var finesse = finesse || {}; 12 finesse.restservices = finesse.restservices || {}; 13 14 /** @private */ 15 finesse.restservices.Contact = finesse.restservices.RestBase.extend(/** @lends finesse.restservices.Contact.prototype */{ 16 17 /** 18 * @class 19 * JavaScript representation of a Contact object. Also exposes 20 * methods to operate on the object against the server. 21 * 22 * @param {Object} options 23 * An object with the following properties:<ul> 24 * <li><b>id:</b> The id of the object being constructed</li> 25 * <li><b>onLoad(this): (optional)</b> when the object is successfully loaded from the server</li> 26 * <li><b>onChange(this): (optional)</b> when an update notification of the object is received</li> 27 * <li><b>onAdd(this): (optional)</b> when a notification that the object is created is received</li> 28 * <li><b>onDelete(this): (optional)</b> when a notification that the object is deleted is received</li> 29 * <li><b>onError(rsp): (optional)</b> if loading of the object fails, invoked with the error response object:<ul> 30 * <li><b>status:</b> {Number} The HTTP status code returned</li> 31 * <li><b>content:</b> {String} Raw string of response</li> 32 * <li><b>object:</b> {Object} Parsed object of response</li> 33 * <li><b>error:</b> {Object} Wrapped exception that was caught:<ul> 34 * <li><b>errorType:</b> {String} Type of error that was caught</li> 35 * <li><b>errorMessage:</b> {String} Message associated with error</li> 36 * </ul></li> 37 * </ul></li> 38 * <li><b>parentObj: (optional)</b> The parent object</li></ul> 39 * @constructs finesse.restservices.Contact 40 **/ 41 init: function (options) { 42 this._super(options); 43 }, 44 45 /** 46 * @private 47 * Gets the REST class for the current object - this is the Contact class. 48 * @returns {Object} The Contact class. 49 */ 50 getRestClass: function () { 51 return finesse.restservices.Contact; 52 }, 53 54 /** 55 * @private 56 * Gets the REST type for the current object - this is a "Contact". 57 * @returns {String} The Contact string. 58 */ 59 getRestType: function () { 60 return "Contact"; 61 }, 62 63 /** 64 * @private 65 * Override default to indicate that this object doesn't support making 66 * requests. 67 */ 68 supportsRequests: false, 69 70 /** 71 * @private 72 * Override default to indicate that this object doesn't support subscriptions. 73 */ 74 supportsSubscriptions: false, 75 76 /** 77 * Getter for the firstName. 78 * @returns {String} The firstName. 79 */ 80 getFirstName: function () { 81 this.isLoaded(); 82 return this.getData().firstName; 83 }, 84 85 /** 86 * Getter for the lastName. 87 * @returns {String} The lastName. 88 */ 89 getLastName: function () { 90 this.isLoaded(); 91 return this.getData().lastName; 92 }, 93 94 /** 95 * Getter for the phoneNumber. 96 * @returns {String} The phoneNumber. 97 */ 98 getPhoneNumber: function () { 99 this.isLoaded(); 100 return this.getData().phoneNumber; 101 }, 102 103 /** 104 * Getter for the description. 105 * @returns {String} The description. 106 */ 107 getDescription: function () { 108 this.isLoaded(); 109 return this.getData().description; 110 }, 111 112 createPutSuccessHandler: function(contact, contentBody, successHandler){ 113 return function (rsp) { 114 // Update internal structure based on response. Here we 115 // inject the contentBody from the PUT request into the 116 // rsp.object element to mimic a GET as a way to take 117 // advantage of the existing _processResponse method. 118 rsp.object = contentBody; 119 contact._processResponse(rsp); 120 121 //Remove the injected Contact object before cascading response 122 rsp.object = {}; 123 124 //cascade response back to consumer's response handler 125 successHandler(rsp); 126 }; 127 }, 128 129 createPostSuccessHandler: function (contact, contentBody, successHandler) { 130 return function (rsp) { 131 rsp.object = contentBody; 132 contact._processResponse(rsp); 133 134 //Remove the injected Contact object before cascading response 135 rsp.object = {}; 136 137 //cascade response back to consumer's response handler 138 successHandler(rsp); 139 }; 140 }, 141 142 /** 143 * Add 144 */ 145 add: function (newValues, handlers) { 146 // this.isLoaded(); 147 var contentBody = {}; 148 149 contentBody[this.getRestType()] = { 150 "firstName": newValues.firstName, 151 "lastName": newValues.lastName, 152 "phoneNumber": newValues.phoneNumber, 153 "description": newValues.description 154 }; 155 156 // Protect against null dereferencing of options allowing its (nonexistant) keys to be read as undefined 157 handlers = handlers || {}; 158 159 this.restRequest(this.getRestUrl(), { 160 method: 'POST', 161 success: this.createPostSuccessHandler(this, contentBody, handlers.success), 162 error: handlers.error, 163 content: contentBody 164 }); 165 166 return this; // Allow cascading 167 }, 168 169 /** 170 * Update 171 */ 172 update: function (newValues, handlers) { 173 this.isLoaded(); 174 var contentBody = {}; 175 176 contentBody[this.getRestType()] = { 177 "uri": this.getId(), 178 "firstName": newValues.firstName, 179 "lastName": newValues.lastName, 180 "phoneNumber": newValues.phoneNumber, 181 "description": newValues.description 182 }; 183 184 // Protect against null dereferencing of options allowing its (nonexistant) keys to be read as undefined 185 handlers = handlers || {}; 186 187 this.restRequest(this.getRestUrl(), { 188 method: 'PUT', 189 success: this.createPutSuccessHandler(this, contentBody, handlers.success), 190 error: handlers.error, 191 content: contentBody 192 }); 193 194 return this; // Allow cascading 195 }, 196 197 198 /** 199 * Delete 200 */ 201 "delete": function ( handlers) { 202 this.isLoaded(); 203 204 // Protect against null dereferencing of options allowing its (nonexistent) keys to be read as undefined 205 handlers = handlers || {}; 206 207 this.restRequest(this.getRestUrl(), { 208 method: 'DELETE', 209 success: this.createPutSuccessHandler(this, {}, handlers.success), 210 error: handlers.error, 211 content: undefined 212 }); 213 214 return this; // Allow cascading 215 } 216 }); 217 218