Source: store.js

  1. /**
  2. * A factory function for returning an object capable of storing objects for
  3. * retrieval later by the application.
  4. *
  5. * @param {Database} database
  6. * The database this store is for.
  7. * @return {Object} -
  8. * An object with put, remove, and all functions.
  9. */
  10. Rekord.store = function(database)
  11. {
  12. return {
  13. /**
  14. * Places a record in the store with the given key.
  15. *
  16. * @param {String|Number} key
  17. * The key to store the record as.
  18. * @param {Object} record
  19. * The record to store.
  20. * @param {function} success
  21. * A function to invoke when the record is successfully stored with
  22. * the key. The arguments of the function should be the key and
  23. * record passed to this function.
  24. * @param {function} failure
  25. * A function to invoke when the record failed to be stored with the
  26. * key. The arguments of the function should be the key, record, and
  27. * an error that occurred if available.
  28. */
  29. put: function(key, record, success, failure)
  30. {
  31. success( key, record );
  32. },
  33. // TODO
  34. get: function(key, success, failure)
  35. {
  36. failure( key, void 0 );
  37. },
  38. /**
  39. * Removes a record from the store with the given key.
  40. *
  41. * @param {String|Number} key
  42. * The key to remove from the store.
  43. * @param {function} success
  44. * A function to invoke when the record doesn't exist in the store.
  45. * The arguments of the function are the removedValue (if any) and
  46. * the key passed to this function.
  47. * @param {function} failure
  48. * A function to invoke when there was an issue removing the key
  49. * from the store. The arguments of the function are the key given
  50. * to this function and an error that occurred if available.
  51. */
  52. remove: function(key, success, failure)
  53. {
  54. success( key );
  55. },
  56. /**
  57. * Returns all records and their keys to the given success callback.
  58. *
  59. * @param {function} success
  60. * The function to invoke with the array of records and an array
  61. * of keys.
  62. * @param {function} failure
  63. * The function to invoke with the error that occurred if available.
  64. */
  65. all: function(success, failure)
  66. {
  67. success( [], [] );
  68. },
  69. /**
  70. * Resets the store so it contains ONLY the given keys & record pairs.
  71. *
  72. * @param {String[]} keys -
  73. * The array of keys.
  74. * @param {Object[]} records -
  75. * The array of records to save.
  76. * @param {function} success
  77. * The function to invoke with the array of records and an array
  78. * of keys.
  79. * @param {function} failure
  80. * The function to invoke with the error that occurred if available.
  81. */
  82. reset: function(keys, records, success, failure)
  83. {
  84. success( keys, records );
  85. }
  86. };
  87. };
  88. /**
  89. * Sets the store implementation provided the factory function. This function
  90. * can only be called once - all subsequent calls will be ignored unless
  91. * `overwrite` is given as a truthy value.
  92. *
  93. * @memberof Rekord
  94. * @param {Function} factory -
  95. * The factory which provides store implementations.
  96. * @param {Boolean} [overwrite=false] -
  97. * True if existing implementations are to be ignored and the given factory
  98. * should be the implementation.
  99. */
  100. Rekord.setStore = function(factory, overwrite)
  101. {
  102. if ( !Rekord.storeSet || overwrite )
  103. {
  104. Rekord.store = factory;
  105. Rekord.storeSet = true;
  106. }
  107. };