[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * @class Ext.data.Api |
---|
| 10 | * @extends Object |
---|
| 11 | * Ext.data.Api is a singleton designed to manage the data API including methods |
---|
| 12 | * for validating a developer's DataProxy API. Defines variables for CRUD actions |
---|
| 13 | * create, read, update and destroy in addition to a mapping of RESTful HTTP methods |
---|
| 14 | * GET, POST, PUT and DELETE to CRUD actions. |
---|
| 15 | * @singleton |
---|
| 16 | */ |
---|
| 17 | Ext.data.Api = (function() { |
---|
| 18 | |
---|
| 19 | // private validActions. validActions is essentially an inverted hash of Ext.data.Api.actions, where value becomes the key. |
---|
| 20 | // Some methods in this singleton (e.g.: getActions, getVerb) will loop through actions with the code <code>for (var verb in this.actions)</code> |
---|
| 21 | // For efficiency, some methods will first check this hash for a match. Those methods which do acces validActions will cache their result here. |
---|
| 22 | // We cannot pre-define this hash since the developer may over-ride the actions at runtime. |
---|
| 23 | var validActions = {}; |
---|
| 24 | |
---|
| 25 | return { |
---|
| 26 | /** |
---|
| 27 | * Defined actions corresponding to remote actions: |
---|
| 28 | * <pre><code> |
---|
| 29 | actions: { |
---|
| 30 | create : 'create', // Text representing the remote-action to create records on server. |
---|
| 31 | read : 'read', // Text representing the remote-action to read/load data from server. |
---|
| 32 | update : 'update', // Text representing the remote-action to update records on server. |
---|
| 33 | destroy : 'destroy' // Text representing the remote-action to destroy records on server. |
---|
| 34 | } |
---|
| 35 | * </code></pre> |
---|
| 36 | * @property actions |
---|
| 37 | * @type Object |
---|
| 38 | */ |
---|
| 39 | actions : { |
---|
| 40 | create : 'create', |
---|
| 41 | read : 'read', |
---|
| 42 | update : 'update', |
---|
| 43 | destroy : 'destroy' |
---|
| 44 | }, |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Defined {CRUD action}:{HTTP method} pairs to associate HTTP methods with the |
---|
| 48 | * corresponding actions for {@link Ext.data.DataProxy#restful RESTful proxies}. |
---|
| 49 | * Defaults to: |
---|
| 50 | * <pre><code> |
---|
| 51 | restActions : { |
---|
| 52 | create : 'POST', |
---|
| 53 | read : 'GET', |
---|
| 54 | update : 'PUT', |
---|
| 55 | destroy : 'DELETE' |
---|
| 56 | }, |
---|
| 57 | * </code></pre> |
---|
| 58 | */ |
---|
| 59 | restActions : { |
---|
| 60 | create : 'POST', |
---|
| 61 | read : 'GET', |
---|
| 62 | update : 'PUT', |
---|
| 63 | destroy : 'DELETE' |
---|
| 64 | }, |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Returns true if supplied action-name is a valid API action defined in <code>{@link #actions}</code> constants |
---|
| 68 | * @param {String} action Action to test for availability. |
---|
| 69 | * @return {Boolean} |
---|
| 70 | */ |
---|
| 71 | isAction : function(action) { |
---|
| 72 | return (Ext.data.Api.actions[action]) ? true : false; |
---|
| 73 | }, |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Returns the actual CRUD action KEY "create", "read", "update" or "destroy" from the supplied action-name. This method is used internally and shouldn't generally |
---|
| 77 | * need to be used directly. The key/value pair of Ext.data.Api.actions will often be identical but this is not necessarily true. A developer can override this naming |
---|
| 78 | * convention if desired. However, the framework internally calls methods based upon the KEY so a way of retreiving the the words "create", "read", "update" and "destroy" is |
---|
| 79 | * required. This method will cache discovered KEYS into the private validActions hash. |
---|
| 80 | * @param {String} name The runtime name of the action. |
---|
| 81 | * @return {String||null} returns the action-key, or verb of the user-action or null if invalid. |
---|
| 82 | * @nodoc |
---|
| 83 | */ |
---|
| 84 | getVerb : function(name) { |
---|
| 85 | if (validActions[name]) { |
---|
| 86 | return validActions[name]; // <-- found in cache. return immediately. |
---|
| 87 | } |
---|
| 88 | for (var verb in this.actions) { |
---|
| 89 | if (this.actions[verb] === name) { |
---|
| 90 | validActions[name] = verb; |
---|
| 91 | break; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | return (validActions[name] !== undefined) ? validActions[name] : null; |
---|
| 95 | }, |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Returns true if the supplied API is valid; that is, check that all keys match defined actions |
---|
| 99 | * otherwise returns an array of mistakes. |
---|
| 100 | * @return {String[]|true} |
---|
| 101 | */ |
---|
| 102 | isValid : function(api){ |
---|
| 103 | var invalid = []; |
---|
| 104 | var crud = this.actions; // <-- cache a copy of the actions. |
---|
| 105 | for (var action in api) { |
---|
| 106 | if (!(action in crud)) { |
---|
| 107 | invalid.push(action); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | return (!invalid.length) ? true : invalid; |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Returns true if the supplied verb upon the supplied proxy points to a unique url in that none of the other api-actions |
---|
| 115 | * point to the same url. The question is important for deciding whether to insert the "xaction" HTTP parameter within an |
---|
| 116 | * Ajax request. This method is used internally and shouldn't generally need to be called directly. |
---|
| 117 | * @param {Ext.data.DataProxy} proxy |
---|
| 118 | * @param {String} verb |
---|
| 119 | * @return {Boolean} |
---|
| 120 | */ |
---|
| 121 | hasUniqueUrl : function(proxy, verb) { |
---|
| 122 | var url = (proxy.api[verb]) ? proxy.api[verb].url : null; |
---|
| 123 | var unique = true; |
---|
| 124 | for (var action in proxy.api) { |
---|
| 125 | if ((unique = (action === verb) ? true : (proxy.api[action].url != url) ? true : false) === false) { |
---|
| 126 | break; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | return unique; |
---|
| 130 | }, |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * This method is used internally by <tt>{@link Ext.data.DataProxy DataProxy}</tt> and should not generally need to be used directly. |
---|
| 134 | * Each action of a DataProxy api can be initially defined as either a String or an Object. When specified as an object, |
---|
| 135 | * one can explicitly define the HTTP method (GET|POST) to use for each CRUD action. This method will prepare the supplied API, setting |
---|
| 136 | * each action to the Object form. If your API-actions do not explicitly define the HTTP method, the "method" configuration-parameter will |
---|
| 137 | * be used. If the method configuration parameter is not specified, POST will be used. |
---|
| 138 | <pre><code> |
---|
| 139 | new Ext.data.HttpProxy({ |
---|
| 140 | method: "POST", // <-- default HTTP method when not specified. |
---|
| 141 | api: { |
---|
| 142 | create: 'create.php', |
---|
| 143 | load: 'read.php', |
---|
| 144 | save: 'save.php', |
---|
| 145 | destroy: 'destroy.php' |
---|
| 146 | } |
---|
| 147 | }); |
---|
| 148 | |
---|
| 149 | // Alternatively, one can use the object-form to specify the API |
---|
| 150 | new Ext.data.HttpProxy({ |
---|
| 151 | api: { |
---|
| 152 | load: {url: 'read.php', method: 'GET'}, |
---|
| 153 | create: 'create.php', |
---|
| 154 | destroy: 'destroy.php', |
---|
| 155 | save: 'update.php' |
---|
| 156 | } |
---|
| 157 | }); |
---|
| 158 | </code></pre> |
---|
| 159 | * |
---|
| 160 | * @param {Ext.data.DataProxy} proxy |
---|
| 161 | */ |
---|
| 162 | prepare : function(proxy) { |
---|
| 163 | if (!proxy.api) { |
---|
| 164 | proxy.api = {}; // <-- No api? create a blank one. |
---|
| 165 | } |
---|
| 166 | for (var verb in this.actions) { |
---|
| 167 | var action = this.actions[verb]; |
---|
| 168 | proxy.api[action] = proxy.api[action] || proxy.url || proxy.directFn; |
---|
| 169 | if (typeof(proxy.api[action]) == 'string') { |
---|
| 170 | proxy.api[action] = { |
---|
| 171 | url: proxy.api[action], |
---|
| 172 | method: (proxy.restful === true) ? Ext.data.Api.restActions[action] : undefined |
---|
| 173 | }; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | }, |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * Prepares a supplied Proxy to be RESTful. Sets the HTTP method for each api-action to be one of |
---|
| 180 | * GET, POST, PUT, DELETE according to the defined {@link #restActions}. |
---|
| 181 | * @param {Ext.data.DataProxy} proxy |
---|
| 182 | */ |
---|
| 183 | restify : function(proxy) { |
---|
| 184 | proxy.restful = true; |
---|
| 185 | for (var verb in this.restActions) { |
---|
| 186 | proxy.api[this.actions[verb]].method || |
---|
| 187 | (proxy.api[this.actions[verb]].method = this.restActions[verb]); |
---|
| 188 | } |
---|
| 189 | // TODO: perhaps move this interceptor elsewhere? like into DataProxy, perhaps? Placed here |
---|
| 190 | // to satisfy initial 3.0 final release of REST features. |
---|
| 191 | proxy.onWrite = proxy.onWrite.createInterceptor(function(action, o, response, rs) { |
---|
| 192 | var reader = o.reader; |
---|
| 193 | var res = new Ext.data.Response({ |
---|
| 194 | action: action, |
---|
| 195 | raw: response |
---|
| 196 | }); |
---|
| 197 | |
---|
| 198 | switch (response.status) { |
---|
| 199 | case 200: // standard 200 response, send control back to HttpProxy#onWrite by returning true from this intercepted #onWrite |
---|
| 200 | return true; |
---|
| 201 | break; |
---|
| 202 | case 201: // entity created but no response returned |
---|
| 203 | if (Ext.isEmpty(res.raw.responseText)) { |
---|
| 204 | res.success = true; |
---|
| 205 | } else { |
---|
| 206 | //if the response contains data, treat it like a 200 |
---|
| 207 | return true; |
---|
| 208 | } |
---|
| 209 | break; |
---|
| 210 | case 204: // no-content. Create a fake response. |
---|
| 211 | res.success = true; |
---|
| 212 | res.data = null; |
---|
| 213 | break; |
---|
| 214 | default: |
---|
| 215 | return true; |
---|
| 216 | break; |
---|
| 217 | } |
---|
| 218 | if (res.success === true) { |
---|
| 219 | this.fireEvent("write", this, action, res.data, res, rs, o.request.arg); |
---|
| 220 | } else { |
---|
| 221 | this.fireEvent('exception', this, 'remote', action, o, res, rs); |
---|
| 222 | } |
---|
| 223 | o.request.callback.call(o.request.scope, res.data, res, res.success); |
---|
| 224 | |
---|
| 225 | return false; // <-- false to prevent intercepted function from running. |
---|
| 226 | }, proxy); |
---|
| 227 | } |
---|
| 228 | }; |
---|
| 229 | })(); |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * Ext.data.Response |
---|
| 233 | * Experimental. Do not use directly. |
---|
| 234 | */ |
---|
| 235 | Ext.data.Response = function(params, response) { |
---|
| 236 | Ext.apply(this, params, { |
---|
| 237 | raw: response |
---|
| 238 | }); |
---|
| 239 | }; |
---|
| 240 | Ext.data.Response.prototype = { |
---|
| 241 | message : null, |
---|
| 242 | success : false, |
---|
| 243 | status : null, |
---|
| 244 | root : null, |
---|
| 245 | raw : null, |
---|
| 246 | |
---|
| 247 | getMessage : function() { |
---|
| 248 | return this.message; |
---|
| 249 | }, |
---|
| 250 | getSuccess : function() { |
---|
| 251 | return this.success; |
---|
| 252 | }, |
---|
| 253 | getStatus : function() { |
---|
| 254 | return this.status; |
---|
| 255 | }, |
---|
| 256 | getRoot : function() { |
---|
| 257 | return this.root; |
---|
| 258 | }, |
---|
| 259 | getRawResponse : function() { |
---|
| 260 | return this.raw; |
---|
| 261 | } |
---|
| 262 | }; |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * @class Ext.data.Api.Error |
---|
| 266 | * @extends Ext.Error |
---|
| 267 | * Error class for Ext.data.Api errors |
---|
| 268 | */ |
---|
| 269 | Ext.data.Api.Error = Ext.extend(Ext.Error, { |
---|
| 270 | constructor : function(message, arg) { |
---|
| 271 | this.arg = arg; |
---|
| 272 | Ext.Error.call(this, message); |
---|
| 273 | }, |
---|
| 274 | name: 'Ext.data.Api' |
---|
| 275 | }); |
---|
| 276 | Ext.apply(Ext.data.Api.Error.prototype, { |
---|
| 277 | lang: { |
---|
| 278 | 'action-url-undefined': 'No fallback url defined for this action. When defining a DataProxy api, please be sure to define an url for each CRUD action in Ext.data.Api.actions or define a default url in addition to your api-configuration.', |
---|
| 279 | 'invalid': 'received an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions defined in Ext.data.Api.actions', |
---|
| 280 | 'invalid-url': 'Invalid url. Please review your proxy configuration.', |
---|
| 281 | 'execute': 'Attempted to execute an unknown action. Valid API actions are defined in Ext.data.Api.actions"' |
---|
| 282 | } |
---|
| 283 | }); |
---|
| 284 | |
---|
| 285 | |
---|