PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/js/main.js

https://github.com/kuzbida/backgrid_test
JavaScript | 161 lines | 82 code | 20 blank | 59 comment | 4 complexity | 4742b1dbd38ae26a8fd4f521f671dbb6 MD5 | raw file
Possible License(s): MIT
  1. /*global define*/
  2. define([
  3. 'underscore',
  4. 'backbone'
  5. ], function (_, Backbone) {
  6. 'use strict';
  7. var GroupModel = Backbone.Model.extend({
  8. url: '../../json/groups.json',
  9. initialize: function() {
  10. },
  11. defaults: {
  12. id: '',
  13. courseId: '',
  14. teacherId: '',
  15. studentId: '',
  16. name: '',
  17. description: ''
  18. },
  19. validate: function(attributes) {
  20. if ( !attributes.id ){
  21. return 'Fill id!'};
  22. if (!attributes.courseId){
  23. return 'Fill courseId!'};
  24. if (!(attributes.courseId%1)==0){
  25. return 'Course ID must be integer!'};
  26. if (!_.isString('attributes.name')) {
  27. return 'Name must be string!'};
  28. }
  29. })
  30. },
  31. parse: function(response, options) {
  32. return response;
  33. });
  34. return GroupModel;
  35. });
  36. return GroupModel;
  37. });
  38. define([
  39. 'underscore',
  40. 'backbone',
  41. 'models/groups'
  42. ], function (_, Backbone, GroupModel) {
  43. 'use strict';
  44. var GroupsCollection = Backbone.Collection.extend({
  45. model: GroupsModel,
  46. url:'../../json/groups.json'
  47. });
  48. return GroupsCollection;
  49. });
  50. var columns = [{
  51. name: "id", // The key of the model attribute
  52. label: "ID", // The name to display in the header
  53. editable: false, // By default every cell in a column is editable, but *ID* shouldn't be
  54. // Defines a cell type, and ID is displayed as an integer without the ',' separating 1000s.
  55. cell: Backgrid.IntegerCell.extend({
  56. orderSeparator: ''
  57. })
  58. }, {
  59. name: "courseId",
  60. label: "Course",
  61. // The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string
  62. cell: "integer" // An integer cell is a number cell that displays humanized integers
  63. }, {
  64. name: "teacherId",
  65. label: "Teacher",
  66. cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
  67. }, {
  68. name: "studentId",
  69. label: "Group head",
  70. cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
  71. //"number" // A cell type for floating point value, defaults to have a precision 2 decimal numbers
  72. }, {
  73. name: "name",
  74. label: "Name",
  75. cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
  76. }, {
  77. name: "description",
  78. label: "Description",
  79. cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
  80. }];
  81. // Initialize a new Grid instance
  82. var grid = new Backgrid.Grid({
  83. columns: columns,
  84. collection: GroupsCollection
  85. });
  86. // Render the grid and attach the root to your HTML document
  87. $("#example-1-result").append(grid.render().el);
  88. // Fetch some countries from the url
  89. GroupsCollection.fetch({reset: true});
  90. /*
  91. var GroupsCollection = Backbone.Collection.extend({
  92. model: GroupModel,
  93. url: "../json/groups.json",
  94. state: {
  95. pageSize: 15
  96. },
  97. mode: "client" // page entirely on the client side
  98. });
  99. var groupsCollection = new GroupsCollection();
  100. // Set up a grid to use the pageable collection
  101. var groupGrid = new Backgrid.Grid({
  102. columns: [{
  103. // enable the select-all extension
  104. name: "",
  105. cell: "select-row",
  106. headerCell: "select-all"
  107. }].concat(columns),
  108. collection: GroupsCollection
  109. });
  110. // Render the grid
  111. var $example2 = $("#example-2-result");
  112. $example2.append(pageableGrid.render().el)
  113. // Initialize the paginator
  114. var paginator = new Backgrid.Extension.Paginator({
  115. collection: GroupsCollection
  116. });
  117. // Render the paginator
  118. $example2.after(paginator.render().el);
  119. // Initialize a client-side filter to filter on the client
  120. // mode pageable collection's cache.
  121. var filter = new Backgrid.Extension.ClientSideFilter({
  122. collection: GroupsCollection,
  123. fields: ['name']
  124. });
  125. // Render the filter
  126. $example2.before(filter.render().el);
  127. // Add some space to the filter and move it to the right
  128. $(filter.el).css({float: "right", margin: "20px"});
  129. // Fetch some data
  130. GroupsCollection.fetch({reset: true});
  131. */