PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/MVC/js/pieces/myNestedCollection.js

https://github.com/GuggerJoel/mmready-test
JavaScript | 118 lines | 41 code | 12 blank | 65 comment | 4 complexity | f4cb5776e6f4e674834f9381b85071a3 MD5 | raw file
  1. //***************************************************************************//
  2. //******************* Model Nested Collection ***********************//
  3. //***************************************************************************//
  4. var MyModelNestedCollection = MyModel.extend({
  5. nested: 'collection',
  6. initialize: function (attrs, options) {
  7. // Si la "nested" col. n'est pas fournie à la création, on la crée vide
  8. if (typeof attrs[this.nested] == "undefined") {
  9. attrs[this.nested] = [];
  10. }
  11. // Création de la nested collection
  12. this.set(this.nested, new window[this.nested.capitalize()](
  13. attrs[this.nested]
  14. ));
  15. // Propagation des events de la nested col. sur le model en cours (this)
  16. this.get(this.nested).on('all', function(eventName) {
  17. this.trigger(eventName, this);
  18. }, this);
  19. },
  20. toJSON: function(attrs, options) {
  21. // Rajout du JSON de la nested col. au JSON du model en cours (this)
  22. var colObj = {};
  23. colObj[this.nested] = this.get(this.nested).toJSON();
  24. return _.extend(
  25. Backbone.Model.prototype.toJSON.apply(this, arguments),
  26. colObj
  27. );
  28. },
  29. parse: function (response) {
  30. response = MyModel.prototype.parse.apply(this, arguments);
  31. if (typeof response[this.nested] == "undefined") {
  32. response[this.nested] = [];
  33. }
  34. // Création de la nested collection
  35. response[this.nested] = new window[this.nested.capitalize()](
  36. response[this.nested]
  37. );
  38. return response;
  39. }
  40. });
  41. var Event = MyModelNestedCollection.extend({
  42. nested: 'artists'
  43. // defaults: function () {return {
  44. // instruments: new Instruments()
  45. // }},
  46. // initialize: function(attrs, options){ //CHabloz n'a pas ça
  47. //
  48. // MyModelNestedCollection.prototype.initialize.apply(this, arguments),
  49. // console.log("Nouveau Musician créé. Name: "+this.get('name'));
  50. // }
  51. });
  52. var Artist = MyModelNestedCollection.extend({
  53. nested: 'musicians'
  54. // defaults: function () {return {
  55. // instruments: new Instruments()
  56. // }},
  57. // initialize: function(attrs, options){ //CHabloz n'a pas ça
  58. //
  59. // MyModelNestedCollection.prototype.initialize.apply(this, arguments),
  60. // console.log("Nouveau Musician créé. Name: "+this.get('name'));
  61. // }
  62. });
  63. var Musician = MyModelNestedCollection.extend({
  64. nested: 'instruments'
  65. // defaults: function () {return {
  66. // instruments: new Instruments()
  67. // }},
  68. // initialize: function(attrs, options){ //CHabloz n'a pas ça
  69. //
  70. // MyModelNestedCollection.prototype.initialize.apply(this, arguments),
  71. // console.log("Nouveau Musician créé. Name: "+this.get('name'));
  72. // }
  73. });
  74. //var MyModelNestedCollection = MyModel2.extend({
  75. // nested: 'collection',
  76. // initialize: function (attrs, options) { // universalisation des noms de la variable contenant la collection
  77. //
  78. // console.log(attrs[this.nested].toJSON());
  79. // console.log(JSON.stringify(attrs[this.nested]));
  80. //
  81. // if (typeof attrs[this.nested] !== "undefined") { //SI le type de l'attribut
  82. // // de ce nom n'est pas indéfini '
  83. // this.set(this.nested, new window[this.nested.capitalize()](attrs[this.nested])); // ALORS crée une instance portant
  84. // // le nom en lui ajoutant
  85. // // une majuscule sur le premier
  86. // // caractère
  87. // }else{
  88. // attrs[this.nested] = [];
  89. // }
  90. //// this.get(this.nested).on('all', function(eventName) {
  91. //// this.trigger(eventName, this);
  92. //// }, this);
  93. // },
  94. // toJSON: function(attrs, options) {//surcharge de la fonction JSON pour stringifyer une collection contenue dans un attribut d'un model
  95. // if(this.nested === 'undefined'){
  96. // console.log('this.nested ( '+this.name+' ) is undefined');
  97. // return;
  98. // }
  99. // var colObj = {};
  100. // colObj[this.nested] = this.get(this.nested).toJSON();
  101. // return _.extend(
  102. // Backbone.Model.prototype.toJSON.apply(this, arguments),
  103. // colObj
  104. // );
  105. // }
  106. //});