PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/column.js

https://github.com/antonyraj/backgrid
JavaScript | 85 lines | 24 code | 8 blank | 53 comment | 1 complexity | 53c0b25d4d3c220df90c99889d3bb4f8 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. /**
  8. A Column is a placeholder for column metadata.
  9. You usually don't need to create an instance of this class yourself as a
  10. collection of column instances will be created for you from a list of column
  11. attributes in the Backgrid.js view class constructors.
  12. @class Backgrid.Column
  13. @extends Backbone.Model
  14. */
  15. var Column = Backgrid.Column = Backbone.Model.extend({
  16. defaults: {
  17. name: undefined,
  18. label: undefined,
  19. sortable: true,
  20. editable: true,
  21. renderable: true,
  22. formatter: undefined,
  23. cell: undefined,
  24. headerCell: undefined
  25. },
  26. /**
  27. Initializes this Column instance.
  28. @param {Object} attrs Column attributes.
  29. @param {string} attrs.name The name of the model attribute.
  30. @param {string|Backgrid.Cell} attrs.cell The cell type.
  31. If this is a string, the capitalized form will be used to look up a
  32. cell class in Backbone, i.e.: string => StringCell. If a Cell subclass
  33. is supplied, it is initialized with a hash of parameters. If a Cell
  34. instance is supplied, it is used directly.
  35. @param {string|Backgrid.HeaderCell} [attrs.headerCell] The header cell type.
  36. @param {string} [attrs.label] The label to show in the header.
  37. @param {boolean} [attrs.sortable=true]
  38. @param {boolean} [attrs.editable=true]
  39. @param {boolean} [attrs.renderable=true]
  40. @param {Backgrid.CellFormatter|Object|string} [attrs.formatter] The
  41. formatter to use to convert between raw model values and user input.
  42. @throws {TypeError} If attrs.cell or attrs.options are not supplied.
  43. @throws {ReferenceError} If attrs.cell is a string but a cell class of
  44. said name cannot be found in the Backgrid module.
  45. See:
  46. - Backgrid.Cell
  47. - Backgrid.CellFormatter
  48. */
  49. initialize: function (attrs) {
  50. Backgrid.requireOptions(attrs, ["cell", "name"]);
  51. if (!this.has("label")) {
  52. this.set({ label: this.get("name") }, { silent: true });
  53. }
  54. var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
  55. var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
  56. this.set({ cell: cell, headerCell: headerCell }, { silent: true });
  57. }
  58. });
  59. /**
  60. A Backbone collection of Column instances.
  61. @class Backgrid.Columns
  62. @extends Backbone.Collection
  63. */
  64. var Columns = Backgrid.Columns = Backbone.Collection.extend({
  65. /**
  66. @property {Backgrid.Column} model
  67. */
  68. model: Column
  69. });