PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/MVC/js/pourChabloz.js

https://github.com/GuggerJoel/mmready-test
JavaScript | 147 lines | 90 code | 27 blank | 30 comment | 4 complexity | 695e64874f65eb979abfa4f2e442f01a MD5 | raw file
  1. ////***************************************************************************//
  2. //******************* Model Extension ***********************//
  3. //***************************************************************************//
  4. var MyModel2 = Backbone.Model.extend();
  5. String.prototype.capitalize = function() {
  6. return this.charAt(0).toUpperCase() + this.slice(1);
  7. };
  8. var MyModelNestedCollection = MyModel2.extend({
  9. nested: 'collection',
  10. initialize: function (attrs, options) { // universalisation des noms de la variable contenant la collection
  11. console.log(attrs[this.nested].toJSON());
  12. console.log(JSON.stringify(attrs[this.nested]));
  13. if (typeof attrs[this.nested] !== "undefined") { //SI le type de l'attribut
  14. // de ce nom n'est pas indéfini '
  15. this.set(this.nested, new window[this.nested.capitalize()](attrs[this.nested])); // ALORS crée une instance portant
  16. // le nom en lui ajoutant
  17. // une majuscule sur le premier
  18. // caractère
  19. }else{
  20. attrs[this.nested] = [];
  21. }
  22. // this.get(this.nested).on('all', function(eventName) {
  23. // this.trigger(eventName, this);
  24. // }, this);
  25. },
  26. toJSON: function(attrs, options) {//surcharge de la fonction JSON pour stringifyer une collection contenue dans un attribut d'un model
  27. if(this.nested === 'undefined'){
  28. console.log('this.nested ( '+this.name+' ) is undefined');
  29. return;
  30. }
  31. var colObj = {};
  32. colObj[this.nested] = this.get(this.nested).toJSON();
  33. return _.extend(
  34. Backbone.Model.prototype.toJSON.apply(this, arguments),
  35. colObj
  36. );
  37. }
  38. });
  39. var Instrument = MyModel2.extend({
  40. //urlRoot: URLSERVEURinstruSuccess,
  41. initialize: function(){
  42. console.log("Nouvel Instrument créé. Name: "+this.get('name'));
  43. this.on('change', function(event){
  44. console.log('Un événement "change" est survenu sur '+JSON.stringify(this.changed)+'. L objet entier en JSON:' + JSON.stringify(event));
  45. return this;
  46. });
  47. this.on('invalid', function(model, error){//si la méthode validate perçoit un truc pas valide elle génère un évènement "invalid"
  48. console.log("Message d'erreur de validation: "+ error); // error contient la string qui est "returnée" par la fonction validate
  49. });
  50. }
  51. // ,
  52. // printDetails: function(){
  53. // console.log("Instrument Name: "+this.get('name'));
  54. // },
  55. // validate: function(attrs){ //ou validateName, validateType, ... : pour valider qu'un attr
  56. // if(!attrs.name){
  57. // return "Ein Name einsetzen";
  58. // }
  59. // }
  60. });
  61. var Musician = MyModelNestedCollection.extend({
  62. nested: 'instruments',
  63. // defaults: function () {return {
  64. // instruments: new Instruments()
  65. // }},
  66. initialize: function(attrs, options){
  67. MyModelNestedCollection.prototype.initialize.apply(this, arguments),
  68. console.log("Nouveau Musician créé. Name: "+this.get('name'));
  69. }
  70. });
  71. //***************************************************************************//
  72. //******************* Collection ***********************//
  73. //***************************************************************************//
  74. var MyCollection = Backbone.Collection.extend();
  75. var Musicians = MyCollection.extend({
  76. url: URLSERVEURMusicianSuccess, //MUSICIANS
  77. model:Musician,
  78. parse: function (attrs, opt) {
  79. console.log("Into parse function of Musicians");
  80. console.log("If typeof attrs != 'undefined'");
  81. if (typeof attrs != "undefined") {
  82. console.log("THEN");
  83. console.log("attrs.data is");
  84. console.log(attrs.data);
  85. return attrs.data;
  86. }else{
  87. console.log("ELSE");
  88. console.log("attrs.data de parse de Musicians : ");
  89. console.log(attrs.data);
  90. return attrs.data;
  91. }
  92. }
  93. });
  94. var Instruments = MyCollection.extend({
  95. model: Instrument
  96. });
  97. ////***************************************************************************//
  98. //******************* Model Extension ***********************//
  99. //***************************************************************************//
  100. var musiciansList = new Musicians({"id":"3",
  101. "name":"Romain",
  102. "stagename":"Remixed",
  103. "created_at":"2014-05-26 14:22:41",
  104. "updated_at":"2014-06-02 09:47:42",
  105. "instruments":[
  106. {"id":"2","musician_id":"3","name":"Guitar"},
  107. {"id":"3","musician_id":"3","name":"Bass"},
  108. {"id":"4","musician_id":"3","name":"Clarinette"}]},
  109. {"id":"4",
  110. "name":"Naty",
  111. "stagename":"Remixed",
  112. "created_at":"2014-05-26 14:22:41",
  113. "updated_at":"2014-06-02 09:47:42",
  114. "instruments":[
  115. {"id":"2","musician_id":"3","name":"Drums"},
  116. {"id":"3","musician_id":"3","name":"Piano"}]});
  117. //var receivedMusicians = musiciansList.fetch();
  118. console.log("Liste de Musicians :");
  119. console.log(receivedMusicians);
  120. console.log(JSON.stringify(receivedMusicians));