/test/src/ligament.js

https://github.com/ashook/ligament.js · JavaScript · 128 lines · 81 code · 22 blank · 25 comment · 8 complexity · 75b4e6407f52854d6e9642d3fd41fc9b MD5 · raw file

  1. // ligament.js -- Associations for Backbone
  2. (function () {
  3. var Ligament;
  4. var _models = {};
  5. var _collections = {};
  6. var _associations = {};
  7. if (typeof exports !== 'undefined') {
  8. Ligament = exports;
  9. } else {
  10. Ligament = this.Ligament = {};
  11. }
  12. // Current version of the library.
  13. Ligament.VERSION = '0.1.0';
  14. Ligament.registerModel = function (name, model) {
  15. _models[name] = model;
  16. if (!model.prototype) model.prototype = {};
  17. model.prototype.modelName = name;
  18. };
  19. Ligament.models = _models;
  20. Ligament.registerCollection = function(name, collection) {
  21. _collections[name] = collection;
  22. if (!collection.prototype) collection.prototype = {};
  23. collection.prototype.collectionName = name;
  24. }
  25. Ligament.collections = _collections;
  26. // ======================================================================
  27. // Associations
  28. Ligament.Associations = {};
  29. // ======================================================================
  30. // BelongsTo
  31. //
  32. // note.get('owner') ==> owners.get(note.get('owner_id'))
  33. Ligament.Associations.BelongsToAssociation = function (modelName, targetName, collectionName, foreignKey) {
  34. return {
  35. associationType: "belongsTo"
  36. , modelName: modelName
  37. , targetName: targetName
  38. , collectionName: collectionName
  39. , foreignKey: foreignKey
  40. , findTarget: function (modelElement) {
  41. return Ligament.collections[collectionName].get(modelElement.get(foreignKey))
  42. }
  43. , toString: function () {
  44. return "BelongsToAssociation: " + modelName + " belongs to " + targetName + " (" + collectionName + ") via " + foreignKey + ".";
  45. }
  46. }
  47. };
  48. // ======================================================================
  49. // DelegatesTo
  50. //
  51. // You must have a belongsTo relationship set up already. You name a
  52. // local attribute, a belongsTo relationship, and the remote
  53. // attribute.
  54. //
  55. // note.get('owner_name') ==> note.get('owner').get('name')
  56. Ligament.Associations.DelegatesToAssociation = function (modelName, targetName, associationName, foreignName) {
  57. return {
  58. associationType: "delegatesTo"
  59. , modelName: modelName
  60. , targetName: targetName
  61. , associationName: associationName
  62. , foreignName: foreignName
  63. , findTarget: function (modelElement) {
  64. return modelElement.get(associationName).get(foreignName);
  65. }
  66. , toString: function () {
  67. return "DelegatesToAssociation: " + modelName + " delegates " + targetName + " to " + associationName + "." + foreignName;
  68. }
  69. }
  70. };
  71. // ======================================================================
  72. // Ligament.Model
  73. Ligament.Model = Backbone.Model.extend({
  74. get: function (attribute) {
  75. var association = _associations[this.modelName];
  76. var finder;
  77. if (association) { finder = association[attribute]; }
  78. if (finder) {
  79. return finder.findTarget(this);
  80. } else {
  81. return Backbone.Model.prototype.get.call(this, attribute);
  82. }
  83. }
  84. });
  85. Ligament.registerAssociation = function (association) {
  86. // console.log("Registering '" + association.toString());
  87. if (!_associations[association.modelName]) _associations[association.modelName] = {};
  88. _associations[association.modelName][association.targetName] = association;
  89. }
  90. Ligament.Model.belongsTo = function (modelName, targetName, collectionName, foreignKey) {
  91. Ligament.registerAssociation(new Ligament.Associations.BelongsToAssociation(modelName, targetName, collectionName, foreignKey));
  92. };
  93. Ligament.Model.delegatesTo = function (modelName, targetName, associationName, foreignName) {
  94. Ligament.registerAssociation(new Ligament.Associations.DelegatesToAssociation(modelName, targetName, associationName, foreignName));
  95. };
  96. // ======================================================================
  97. // Ligament.Collection
  98. Ligament.Collection = Backbone.Collection.extend({
  99. // ;-) - Happy little no-op! Nothing to see here yet.
  100. });
  101. // ======================================================================
  102. // Ligament.View
  103. Ligament.View = Backbone.View.extend({
  104. // ;-) - Happy little no-op! Nothing to see here yet.
  105. });
  106. }());