Source: plugins/grabAll.js

  1. Rekord.on( Rekord.Events.Plugins, function(model, db, options)
  2. {
  3. /**
  4. * Gets all model instances currently loaded, locally loaded, or remotely
  5. * loaded and passes it to the `callback` function.
  6. *
  7. * ```javascript
  8. * var Task = Rekord({
  9. * fields: ['name']
  10. * });
  11. * var tasks = Task.grabAll( function(models) {
  12. * models; // local or remotely loaded if it didn't exist locally.
  13. * })
  14. * ```
  15. *
  16. * @method grabAll
  17. * @memberof Rekord.Model
  18. * @param {Function} callback -
  19. * The function to invoke passing the reference of the model collection
  20. * when it's loaded.
  21. * @param {Object} [context] -
  22. * The context (this) for the callback.
  23. * @return {Rekord.Model} -
  24. * The model collection of it exists locally at the moment, or undefined
  25. * if models haven't been loaded yet.
  26. */
  27. model.grabAll = function( callback, context )
  28. {
  29. var callbackContext = context || this;
  30. var models = db.models;
  31. if ( models.length )
  32. {
  33. callback.call( callbackContext, models );
  34. }
  35. else
  36. {
  37. db.ready(function()
  38. {
  39. if ( models.length )
  40. {
  41. callback.call( callbackContext, models );
  42. }
  43. else
  44. {
  45. db.refresh(function()
  46. {
  47. callback.call( callbackContext, models );
  48. });
  49. }
  50. });
  51. }
  52. return models;
  53. };
  54. });