Source: collections/RelationCollection.js

  1. /**
  2. * An extension of the {@link Rekord.ModelCollection} class for relationships.
  3. *
  4. * @constructor
  5. * @memberof Rekord
  6. * @extends Rekord.ModelCollection
  7. * @param {Rekord.Database} database -
  8. * The database for the models in this collection.
  9. * @param {Rekord.Model} model -
  10. * The model instance all models in this collection are related to.
  11. * @param {Rekord.Relation} relator -
  12. * The relation instance responsible for relating/unrelating models.
  13. * @param {modelInput[]} [models] -
  14. * The initial array of models in this collection.
  15. * @param {Boolean} [remoteData=false] -
  16. * If the models array is from a remote source. Remote sources place the
  17. * model directly into the database while local sources aren't stored in the
  18. * database until they're saved.
  19. */
  20. function RelationCollection(database, model, relator, models, remoteData)
  21. {
  22. this.model = model;
  23. this.relator = relator;
  24. this.init( database, models, remoteData );
  25. }
  26. /**
  27. * The model instance all models in this collection are related to.
  28. *
  29. * @memberof Rekord.RelationCollection#
  30. * @member {Rekord.Model} model
  31. */
  32. /**
  33. * The relation instance responsible for relating/unrelating models.
  34. *
  35. * @memberof Rekord.RelationCollection#
  36. * @member {Rekord.Relation} relator
  37. */
  38. extendArray( ModelCollection, RelationCollection,
  39. {
  40. /**
  41. * Sets the entire set of models which are related. If a model is specified
  42. * that doesn't exist in this collection a relationship is added. If a model
  43. * in this collection is not specified in the `input` the relationship is
  44. * removed. Depending on the relationship, adding and removing relationships
  45. * may result in the saving or deleting of models.
  46. *
  47. * @method
  48. * @memberof Rekord.RelationCollection#
  49. * @param {modelInput|modelInput[]} [input] -
  50. * The model or array of models to relate. If input isn't specified, all
  51. * models currently related are unrelated.
  52. * @return {Rekord.RelationCollection} -
  53. * The reference to this collection.
  54. */
  55. set: function(input)
  56. {
  57. this.relator.set( this.model, input );
  58. return this;
  59. },
  60. /**
  61. * Relates one or more models to this collection's model. If a model is
  62. * specified that is already related then it has no effect.
  63. *
  64. * @method
  65. * @memberof Rekord.RelationCollection#
  66. * @param {modelInput|modelInput[]} input -
  67. * The model or array of models to relate.
  68. * @return {Rekord.RelationCollection} -
  69. * The reference to this collection.
  70. */
  71. relate: function(input)
  72. {
  73. this.relator.relate( this.model, input );
  74. return this;
  75. },
  76. /**
  77. * Unrelates one or more models from this collection's model. If a model is
  78. * specified that is not related then it has no effect. If no models are
  79. * specified then all models in this collection are unrelated.
  80. *
  81. * @method
  82. * @memberof Rekord.RelationCollection#
  83. * @param {modelInput|modelInput[]} input -
  84. * The model or array of models to relate.
  85. * @return {Rekord.RelationCollection} -
  86. * The reference to this collection.
  87. */
  88. unrelate: function(input)
  89. {
  90. this.relator.unrelate( this.model, input );
  91. return this;
  92. },
  93. /**
  94. * Determines whether one or more models all exist in this collection.
  95. *
  96. * @method
  97. * @memberof Rekord.RelationCollection#
  98. * @param {modelInput|modelInput[]} input -
  99. * The model or array of models to check for existence.
  100. * @return {Boolean} -
  101. * True if all models are related - otherwise false.
  102. */
  103. isRelated: function(input)
  104. {
  105. return this.relator.isRelated( this.model, input );
  106. },
  107. /**
  108. * Returns a clone of this collection.
  109. *
  110. * @method
  111. * @memberof Rekord.RelationCollection#
  112. * @return {Rekord.RelationCollection} -
  113. * The reference to a clone collection.
  114. */
  115. clone: function()
  116. {
  117. return new RelationCollection( this.database, this.model, this.relator, this, true );
  118. },
  119. /**
  120. * Returns an empty clone of this collection.
  121. *
  122. * @method
  123. * @memberof Rekord.RelationCollection#
  124. * @return {Rekord.RelationCollection} -
  125. * The reference to a clone collection.
  126. */
  127. cloneEmpty: function()
  128. {
  129. return new RelationCollection( this.database, this.model, this.relator );
  130. }
  131. });