/getstarted/static/js/typeslist.js

https://bitbucket.org/Equintero/getstarted · JavaScript · 120 lines · 104 code · 13 blank · 3 comment · 2 complexity · 844626eb5bcd7b4016f843fdde8619d9 MD5 · raw file

  1. $(function(){
  2. var Type = Backbone.Model.extend({});
  3. var Step = Backbone.Model.extend({});
  4. // -- Views
  5. var TypeView = Backbone.View.extend({
  6. tagName: 'li',
  7. events:{
  8. 'click' : 'showsteps',
  9. },
  10. showsteps: function(){
  11. var that = this;
  12. $('#types-steps').slideUp(100,function(){
  13. var steporder = that.model.get('stepOrder');
  14. var stepCollection = new StepCollection();
  15. var stepCollectionView = new StepCollectionView({collection:stepCollection});
  16. if(steporder.length > 0){
  17. $('#types-steps').html(stepCollectionView.el);
  18. }else{
  19. $('#types-steps').html("No Steps listed.");
  20. }
  21. for (i=0;i<steporder.length; i++){
  22. stepCollection.add(steporder[i]);
  23. }
  24. $('#types-steps').delay(100).slideDown(100);
  25. });
  26. },
  27. render: function(){
  28. var html = "<a href='#' class='step-set-link'>"+this.model.get('title')+"</a>";
  29. $(this.el).html(html);
  30. return this;
  31. }
  32. });
  33. var StepView = Backbone.View.extend({
  34. render: function(){
  35. var html = "" +
  36. "<div class='accordion' id='accordion1'>" +
  37. "<div class='accordion-group'>" +
  38. "<div class='accordion-heading'>" +
  39. "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion1' href='#collapse"+this.model.get('id')+"'>"+this.model.get('stepNumber') +' - '+ this.model.get('step').title+"</a></div>" +
  40. "<div id='collapse"+this.model.get('id')+"' class='accordion-body collapse'>" +
  41. "<div class='accordion-inner'>" + this.model.get('step').description +
  42. "</div>"+
  43. "</div>"+
  44. "</div>"+
  45. "</div>";
  46. $(this.el).html(html);
  47. return this;
  48. }
  49. });
  50. // -- Collection
  51. var TypeCollection = Backbone.Collection.extend({
  52. url: '/type/?format=json',
  53. model: Type,
  54. });
  55. var StepCollection = Backbone.Collection.extend({
  56. model: Step,
  57. comparator: function(step) {
  58. return step.get("stepNumber");
  59. },
  60. });
  61. // -- Collection View
  62. var TypeCollectionView = Backbone.View.extend({
  63. id: 'ul',
  64. className: 'nav',
  65. initialize: function(){
  66. this.collection.on('add', this.addOne, this);
  67. this.collection.bind('reset' , this.addAll, this);
  68. this.collection.fetch();
  69. },
  70. render: function(){
  71. this.addAll();
  72. return this;
  73. },
  74. addOne: function(type){
  75. var typeView = new TypeView({model:type});
  76. this.$el.append(typeView.render().el);
  77. this.$el.append("<li class='divider-vertical'></li>");
  78. },
  79. addAll: function(){
  80. this.$el.html('');
  81. this.collection.forEach(this.addOne, this);
  82. },
  83. });
  84. var StepCollectionView = Backbone.View.extend({
  85. id: '',
  86. className: '',
  87. initialize: function(){
  88. this.collection.on('add', this.addOne, this);
  89. this.collection.bind('reset' , this.addAll, this);
  90. },
  91. render: function(){
  92. this.addAll();
  93. return this;
  94. },
  95. addOne: function(step){
  96. var stepView = new StepView({model:step});
  97. this.$el.append(stepView.render().el);
  98. },
  99. addAll: function(){
  100. this.$el.html('');
  101. this.collection.forEach(this.addOne, this);
  102. },
  103. });
  104. typeCollection = new TypeCollection();
  105. typeCollectionView = new TypeCollectionView({collection:typeCollection});
  106. $('#types-buttons').html(typeCollectionView.el);
  107. });