PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/public/ext-4.0.0/docs/source/WebStorage.html

https://github.com/marekjs/Shacser
HTML | 405 lines | 336 code | 69 blank | 0 comment | 0 complexity | d294856815b06577aec383e12af4dba1 MD5 | raw file
  1. <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.proxy.WebStorage-method-constructor'><span id='Ext-data.proxy.WebStorage'>/**
  2. </span></span> * @author Ed Spencer
  3. * @class Ext.data.proxy.WebStorage
  4. * @extends Ext.data.proxy.Client
  5. *
  6. * &lt;p&gt;WebStorageProxy is simply a superclass for the {@link Ext.data.proxy.LocalStorage localStorage} and
  7. * {@link Ext.data.proxy.SessionStorage sessionStorage} proxies. It uses the new HTML5 key/value client-side storage
  8. * objects to save {@link Ext.data.Model model instances} for offline use.&lt;/p&gt;
  9. *
  10. * @constructor
  11. * Creates the proxy, throws an error if local storage is not supported in the current browser
  12. * @param {Object} config Optional config object
  13. */
  14. Ext.define('Ext.data.proxy.WebStorage', {
  15. extend: 'Ext.data.proxy.Client',
  16. alternateClassName: 'Ext.data.WebStorageProxy',
  17. <span id='Ext-data.proxy.WebStorage-cfg-id'> /**
  18. </span> * @cfg {String} id The unique ID used as the key in which all record data are stored in the local storage object
  19. */
  20. id: undefined,
  21. <span id='Ext-data.proxy.WebStorage-method-constructor'> /**
  22. </span> * @ignore
  23. */
  24. constructor: function(config) {
  25. this.callParent(arguments);
  26. <span id='Ext-data.proxy.WebStorage-property-cache'> /**
  27. </span> * Cached map of records already retrieved by this Proxy - ensures that the same instance is always retrieved
  28. * @property cache
  29. * @type Object
  30. */
  31. this.cache = {};
  32. //&lt;debug&gt;
  33. if (this.getStorageObject() === undefined) {
  34. Ext.Error.raise(&quot;Local Storage is not supported in this browser, please use another type of data proxy&quot;);
  35. }
  36. //&lt;/debug&gt;
  37. //if an id is not given, try to use the store's id instead
  38. this.id = this.id || (this.store ? this.store.storeId : undefined);
  39. //&lt;debug&gt;
  40. if (this.id === undefined) {
  41. Ext.Error.raise(&quot;No unique id was provided to the local storage proxy. See Ext.data.proxy.LocalStorage documentation for details&quot;);
  42. }
  43. //&lt;/debug&gt;
  44. this.initialize();
  45. },
  46. //inherit docs
  47. create: function(operation, callback, scope) {
  48. var records = operation.records,
  49. length = records.length,
  50. ids = this.getIds(),
  51. id, record, i;
  52. operation.setStarted();
  53. for (i = 0; i &lt; length; i++) {
  54. record = records[i];
  55. if (record.phantom) {
  56. record.phantom = false;
  57. id = this.getNextId();
  58. } else {
  59. id = record.getId();
  60. }
  61. this.setRecord(record, id);
  62. ids.push(id);
  63. }
  64. this.setIds(ids);
  65. operation.setCompleted();
  66. operation.setSuccessful();
  67. if (typeof callback == 'function') {
  68. callback.call(scope || this, operation);
  69. }
  70. },
  71. //inherit docs
  72. read: function(operation, callback, scope) {
  73. //TODO: respect sorters, filters, start and limit options on the Operation
  74. var records = [],
  75. ids = this.getIds(),
  76. length = ids.length,
  77. i, recordData, record;
  78. //read a single record
  79. if (operation.id) {
  80. record = this.getRecord(operation.id);
  81. if (record) {
  82. records.push(record);
  83. operation.setSuccessful();
  84. }
  85. } else {
  86. for (i = 0; i &lt; length; i++) {
  87. records.push(this.getRecord(ids[i]));
  88. }
  89. operation.setSuccessful();
  90. }
  91. operation.setCompleted();
  92. operation.resultSet = Ext.create('Ext.data.ResultSet', {
  93. records: records,
  94. total : records.length,
  95. loaded : true
  96. });
  97. if (typeof callback == 'function') {
  98. callback.call(scope || this, operation);
  99. }
  100. },
  101. //inherit docs
  102. update: function(operation, callback, scope) {
  103. var records = operation.records,
  104. length = records.length,
  105. ids = this.getIds(),
  106. record, id, i;
  107. operation.setStarted();
  108. for (i = 0; i &lt; length; i++) {
  109. record = records[i];
  110. this.setRecord(record);
  111. //we need to update the set of ids here because it's possible that a non-phantom record was added
  112. //to this proxy - in which case the record's id would never have been added via the normal 'create' call
  113. id = record.getId();
  114. if (id !== undefined &amp;&amp; Ext.Array.indexOf(ids, id) == -1) {
  115. ids.push(id);
  116. }
  117. }
  118. this.setIds(ids);
  119. operation.setCompleted();
  120. operation.setSuccessful();
  121. if (typeof callback == 'function') {
  122. callback.call(scope || this, operation);
  123. }
  124. },
  125. //inherit
  126. destroy: function(operation, callback, scope) {
  127. var records = operation.records,
  128. length = records.length,
  129. ids = this.getIds(),
  130. //newIds is a copy of ids, from which we remove the destroyed records
  131. newIds = [].concat(ids),
  132. i;
  133. for (i = 0; i &lt; length; i++) {
  134. Ext.Array.remove(newIds, records[i].getId());
  135. this.removeRecord(records[i], false);
  136. }
  137. this.setIds(newIds);
  138. operation.setCompleted();
  139. operation.setSuccessful();
  140. if (typeof callback == 'function') {
  141. callback.call(scope || this, operation);
  142. }
  143. },
  144. <span id='Ext-data.proxy.WebStorage-method-getRecord'> /**
  145. </span> * @private
  146. * Fetches a model instance from the Proxy by ID. Runs each field's decode function (if present) to decode the data
  147. * @param {String} id The record's unique ID
  148. * @return {Ext.data.Model} The model instance
  149. */
  150. getRecord: function(id) {
  151. if (this.cache[id] === undefined) {
  152. var rawData = Ext.decode(this.getStorageObject().getItem(this.getRecordKey(id))),
  153. data = {},
  154. Model = this.model,
  155. fields = Model.prototype.fields.items,
  156. length = fields.length,
  157. i, field, name, record;
  158. for (i = 0; i &lt; length; i++) {
  159. field = fields[i];
  160. name = field.name;
  161. if (typeof field.decode == 'function') {
  162. data[name] = field.decode(rawData[name]);
  163. } else {
  164. data[name] = rawData[name];
  165. }
  166. }
  167. record = new Model(data, id);
  168. record.phantom = false;
  169. this.cache[id] = record;
  170. }
  171. return this.cache[id];
  172. },
  173. <span id='Ext-data.proxy.WebStorage-method-setRecord'> /**
  174. </span> * Saves the given record in the Proxy. Runs each field's encode function (if present) to encode the data
  175. * @param {Ext.data.Model} record The model instance
  176. * @param {String} id The id to save the record under (defaults to the value of the record's getId() function)
  177. */
  178. setRecord: function(record, id) {
  179. if (id) {
  180. record.setId(id);
  181. } else {
  182. id = record.getId();
  183. }
  184. var me = this,
  185. rawData = record.data,
  186. data = {},
  187. model = me.model,
  188. fields = model.prototype.fields.items,
  189. length = fields.length,
  190. i = 0,
  191. field, name, obj, key;
  192. for (; i &lt; length; i++) {
  193. field = fields[i];
  194. name = field.name;
  195. if (typeof field.encode == 'function') {
  196. data[name] = field.encode(rawData[name], record);
  197. } else {
  198. data[name] = rawData[name];
  199. }
  200. }
  201. obj = me.getStorageObject();
  202. key = me.getRecordKey(id);
  203. //keep the cache up to date
  204. me.cache[id] = record;
  205. //iPad bug requires that we remove the item before setting it
  206. obj.removeItem(key);
  207. obj.setItem(key, Ext.encode(data));
  208. },
  209. <span id='Ext-data.proxy.WebStorage-method-removeRecord'> /**
  210. </span> * @private
  211. * Physically removes a given record from the local storage. Used internally by {@link #destroy}, which you should
  212. * use instead because it updates the list of currently-stored record ids
  213. * @param {String|Number|Ext.data.Model} id The id of the record to remove, or an Ext.data.Model instance
  214. */
  215. removeRecord: function(id, updateIds) {
  216. var me = this,
  217. ids;
  218. if (id.isModel) {
  219. id = id.getId();
  220. }
  221. if (updateIds !== false) {
  222. ids = me.getIds();
  223. Ext.Array.remove(ids, id);
  224. me.setIds(ids);
  225. }
  226. me.getStorageObject().removeItem(me.getRecordKey(id));
  227. },
  228. <span id='Ext-data.proxy.WebStorage-method-getRecordKey'> /**
  229. </span> * @private
  230. * Given the id of a record, returns a unique string based on that id and the id of this proxy. This is used when
  231. * storing data in the local storage object and should prevent naming collisions.
  232. * @param {String|Number|Ext.data.Model} id The record id, or a Model instance
  233. * @return {String} The unique key for this record
  234. */
  235. getRecordKey: function(id) {
  236. if (id.isModel) {
  237. id = id.getId();
  238. }
  239. return Ext.String.format(&quot;{0}-{1}&quot;, this.id, id);
  240. },
  241. <span id='Ext-data.proxy.WebStorage-method-getRecordCounterKey'> /**
  242. </span> * @private
  243. * Returns the unique key used to store the current record counter for this proxy. This is used internally when
  244. * realizing models (creating them when they used to be phantoms), in order to give each model instance a unique id.
  245. * @return {String} The counter key
  246. */
  247. getRecordCounterKey: function() {
  248. return Ext.String.format(&quot;{0}-counter&quot;, this.id);
  249. },
  250. <span id='Ext-data.proxy.WebStorage-method-getIds'> /**
  251. </span> * @private
  252. * Returns the array of record IDs stored in this Proxy
  253. * @return {Array} The record IDs. Each is cast as a Number
  254. */
  255. getIds: function() {
  256. var ids = (this.getStorageObject().getItem(this.id) || &quot;&quot;).split(&quot;,&quot;),
  257. length = ids.length,
  258. i;
  259. if (length == 1 &amp;&amp; ids[0] === &quot;&quot;) {
  260. ids = [];
  261. } else {
  262. for (i = 0; i &lt; length; i++) {
  263. ids[i] = parseInt(ids[i], 10);
  264. }
  265. }
  266. return ids;
  267. },
  268. <span id='Ext-data.proxy.WebStorage-method-setIds'> /**
  269. </span> * @private
  270. * Saves the array of ids representing the set of all records in the Proxy
  271. * @param {Array} ids The ids to set
  272. */
  273. setIds: function(ids) {
  274. var obj = this.getStorageObject(),
  275. str = ids.join(&quot;,&quot;);
  276. obj.removeItem(this.id);
  277. if (!Ext.isEmpty(str)) {
  278. obj.setItem(this.id, str);
  279. }
  280. },
  281. <span id='Ext-data.proxy.WebStorage-method-getNextId'> /**
  282. </span> * @private
  283. * Returns the next numerical ID that can be used when realizing a model instance (see getRecordCounterKey). Increments
  284. * the counter.
  285. * @return {Number} The id
  286. */
  287. getNextId: function() {
  288. var obj = this.getStorageObject(),
  289. key = this.getRecordCounterKey(),
  290. last = obj.getItem(key),
  291. ids, id;
  292. if (last === null) {
  293. ids = this.getIds();
  294. last = ids[ids.length - 1] || 0;
  295. }
  296. id = parseInt(last, 10) + 1;
  297. obj.setItem(key, id);
  298. return id;
  299. },
  300. <span id='Ext-data.proxy.WebStorage-method-initialize'> /**
  301. </span> * @private
  302. * Sets up the Proxy by claiming the key in the storage object that corresponds to the unique id of this Proxy. Called
  303. * automatically by the constructor, this should not need to be called again unless {@link #clear} has been called.
  304. */
  305. initialize: function() {
  306. var storageObject = this.getStorageObject();
  307. storageObject.setItem(this.id, storageObject.getItem(this.id) || &quot;&quot;);
  308. },
  309. <span id='Ext-data.proxy.WebStorage-method-clear'> /**
  310. </span> * Destroys all records stored in the proxy and removes all keys and values used to support the proxy from the storage object
  311. */
  312. clear: function() {
  313. var obj = this.getStorageObject(),
  314. ids = this.getIds(),
  315. len = ids.length,
  316. i;
  317. //remove all the records
  318. for (i = 0; i &lt; len; i++) {
  319. this.removeRecord(ids[i]);
  320. }
  321. //remove the supporting objects
  322. obj.removeItem(this.getRecordCounterKey());
  323. obj.removeItem(this.id);
  324. },
  325. <span id='Ext-data.proxy.WebStorage-method-getStorageObject'> /**
  326. </span> * @private
  327. * Abstract function which should return the storage object that data will be saved to. This must be implemented
  328. * in each subclass.
  329. * @return {Object} The storage object
  330. */
  331. getStorageObject: function() {
  332. //&lt;debug&gt;
  333. Ext.Error.raise(&quot;The getStorageObject function has not been defined in your Ext.data.proxy.WebStorage subclass&quot;);
  334. //&lt;/debug&gt;
  335. }
  336. });</pre></pre></body></html>