mixin.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var Utils = require("./../utils")
  2. , HasOne = require('./has-one')
  3. , HasMany = require("./has-many")
  4. , BelongsTo = require("./belongs-to")
  5. /* Defines Mixin for all models. */
  6. var Mixin = module.exports = function(){}
  7. Mixin.hasOne = function(associatedDAOFactory, options) {
  8. // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  9. options = options || {}
  10. options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  11. options.useHooks = options.hooks
  12. // the id is in the foreign table
  13. var association = new HasOne(this, associatedDAOFactory, Utils._.extend(options, this.options))
  14. this.associations[association.associationAccessor] = association.injectAttributes()
  15. association.injectGetter(this.DAO.prototype);
  16. association.injectSetter(this.DAO.prototype);
  17. association.injectCreator(this.DAO.prototype);
  18. return this
  19. }
  20. Mixin.belongsTo = function(associatedDAOFactory, options) {
  21. // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  22. options = options || {}
  23. options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  24. options.useHooks = options.hooks
  25. // the id is in this table
  26. var association = new BelongsTo(this, associatedDAOFactory, Utils._.extend(options, this.options))
  27. this.associations[association.associationAccessor] = association.injectAttributes()
  28. association.injectGetter(this.DAO.prototype)
  29. association.injectSetter(this.DAO.prototype)
  30. association.injectCreator(this.DAO.prototype)
  31. return this
  32. }
  33. Mixin.hasMany = function(associatedDAOFactory, options) {
  34. // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  35. options = options || {}
  36. options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  37. options.useHooks = options.hooks
  38. options = Utils._.extend(options, Utils._.omit(this.options, ['hooks']))
  39. // the id is in the foreign table or in a connecting table
  40. var association = new HasMany(this, associatedDAOFactory, options)
  41. this.associations[association.associationAccessor] = association.injectAttributes()
  42. association.injectGetter(this.DAO.prototype)
  43. association.injectSetter(this.DAO.prototype)
  44. association.injectCreator(this.DAO.prototype)
  45. return this
  46. }
  47. Mixin.getAssociation = function(target, alias) {
  48. for (var associationName in this.associations) {
  49. if (this.associations.hasOwnProperty(associationName)) {
  50. var association = this.associations[associationName]
  51. if (association.target === target && (alias === undefined ? !association.isAliased : association.as === alias)) {
  52. return association
  53. }
  54. }
  55. }
  56. return null
  57. }
  58. Mixin.getAssociationByAlias = function(alias) {
  59. for (var associationName in this.associations) {
  60. if (this.associations.hasOwnProperty(associationName)) {
  61. var association = this.associations[associationName]
  62. if (association.as === alias) {
  63. return association
  64. }
  65. }
  66. }
  67. return null
  68. }
  69. /* example for instance methods:
  70. Mixin.prototype.test = function() {
  71. console.log('asd')
  72. }
  73. */