PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/backbone.html

https://github.com/nunoroxo/jquery-handsontable
HTML | 224 lines | 181 code | 31 blank | 12 comment | 0 complexity | b26b98343bb27826d31bd76ba81b144b MD5 | raw file
Possible License(s): MIT
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Backbone.js - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies apart from jQuery)
  8. -->
  9. <script src="../lib/jquery.min.js"></script>
  10. <script src="../dist/jquery.handsontable.full.js"></script>
  11. <link rel="stylesheet" media="screen" href="../dist/jquery.handsontable.full.css">
  12. <!--
  13. Loading demo dependencies. They are used here only to enhance the examples on this page
  14. -->
  15. <link rel="stylesheet" media="screen" href="css/samples.css">
  16. <script src="js/samples.js"></script>
  17. <script src="js/backbone/lodash.underscore.js"></script>
  18. <script src="js/backbone/backbone.js"></script>
  19. <script src="js/backbone/backbone-relational/backbone-relational.js"></script>
  20. <script src="js/highlight/highlight.pack.js"></script>
  21. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  22. <!--
  23. Facebook open graph. Don't copy this to your project :)
  24. -->
  25. <meta property="og:title" content="Integrate with Backbone.js">
  26. <meta property="og:description"
  27. content="Bind your Backbone collections to Handsontable">
  28. <meta property="og:url" content="http://handsontable.com/demo/backbone.html">
  29. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  30. <meta property="og:image:type" content="image/png">
  31. <meta property="og:image:width" content="409">
  32. <meta property="og:image:height" content="164">
  33. <link rel="canonical" href="http://handsontable.com/demo/backbone.html">
  34. <!--
  35. Google Analytics for GitHub Page. Don't copy this to your project :)
  36. -->
  37. <script src="js/ga.js"></script>
  38. </head>
  39. <body>
  40. <a href="http://github.com/warpech/jquery-handsontable">
  41. <img style="position: absolute; top: 0; right: 0; border: 0;"
  42. src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"/>
  43. </a>
  44. <div id="container">
  45. <div class="columnLayout">
  46. <div class="rowLayout">
  47. <div class="descLayout">
  48. <div class="pad">
  49. <h1><a href="../index.html">Handsontable</a></h1>
  50. <div class="tagline">a minimalistic Excel-like <span class="nobreak">data grid</span> editor
  51. for HTML, JavaScript &amp; jQuery
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="rowLayout">
  57. <div class="descLayout">
  58. <div class="pad">
  59. <h2>Backbone.js</h2>
  60. <p>
  61. <a href="http://backbonejs.org/">Backbone.js</a> is a client-side
  62. MV* framework that can do some pretty smart things
  63. with data going to and coming back from a server, and has a great
  64. event model for keeping multiple views in sync.
  65. </p>
  66. <p>
  67. This little example shows how Backbone Models and Collections can
  68. work with Handsontable. Below, you'll see events firing from
  69. changes in the <code>CarCollection</code> by Handsontable or
  70. otherwise.
  71. </p>
  72. <div id="example1"></div>
  73. <p>
  74. <button name="dump" data-dump="#example1" title="Prints current data source to Firebug/Chrome Dev Tools">
  75. Dump data to console
  76. </button>
  77. <button id="add_car">Simulate a new Car</button>
  78. <br>
  79. <select multiple="multiple" id="example1_events" style="width: 100%; height: 100px"></select>
  80. </p>
  81. <p>
  82. <strong>Please note</strong> that Backbone integration is a work in progress since Handsontable 0.8.14.
  83. The code presented here has 2 known issues:
  84. </p>
  85. <ul>
  86. <li>inserting and removing rows or columns triggers errors, both when using <code>alter</code> method and
  87. the context menu
  88. </li>
  89. <li><code>minSpareRows</code> does not have effect directly after row was added from Backbone (as a
  90. workaround, you would need to call <code>loadData</code> instead of <code>render</code>)
  91. </li>
  92. </ul>
  93. <p>
  94. Both issues will be addressed in future versions of HT. Contributions are welcome!
  95. </p>
  96. </div>
  97. </div>
  98. <div class="codeLayout">
  99. <div class="pad">
  100. <div class="jsFiddle">
  101. <div class="jsFiddleLink">Edit in jsFiddle</div>
  102. </div>
  103. <script>
  104. var CarModel = Backbone.Model.extend({});
  105. var CarCollection = Backbone.Collection.extend({
  106. model: CarModel,
  107. // Backbone.Collection doesn't support `splice`, yet! Easy to add.
  108. splice: hacked_splice
  109. });
  110. var cars = new CarCollection();
  111. // since we're not using a server... make up some data. This will make
  112. // a couple CarModels from these plain old objects
  113. cars.add([
  114. {make: "Dodge", model: "Ram", year: 2012, weight: 6811},
  115. {make: "Toyota", model: "Camry", year: 2012, weight: 3190},
  116. {make: "Smart", model: "Fortwo", year: 2012, weight: 1808}
  117. ]);
  118. var $container = $("#example1");
  119. $container.handsontable({
  120. data: cars,
  121. dataSchema: makeCar,
  122. contextMenu: true,
  123. columns: [
  124. attr("make"),
  125. attr("model"),
  126. attr("year")
  127. ],
  128. colHeaders: ["Make", "Model", "Year"]
  129. //minSpareRows: 1 //see notes on the left for `minSpareRows`
  130. });
  131. // this will log all the Backbone events getting fired!
  132. cars.on("all", log_events)
  133. .on("add", function () {
  134. $container.handsontable("render");
  135. })
  136. .on("remove", function () {
  137. $container.handsontable("render");
  138. });
  139. // you'll have to make something like these until there is a better
  140. // way to use the string notation, i.e. "bb:make"!
  141. // normally, you'd get these from the server with .fetch()
  142. function attr(attr) {
  143. // this lets us remember `attr` for when when it is get/set
  144. return {data: function (car, value) {
  145. if (_.isUndefined(value)) {
  146. return car.get(attr);
  147. }
  148. car.set(attr, value);
  149. }};
  150. }
  151. // just setting `dataSchema: CarModel` would be great, but it is non-
  152. // trivial to detect constructors...
  153. function makeCar() {
  154. return new CarModel();
  155. }
  156. // use the "good" Collection methods to emulate Array.splice
  157. function hacked_splice(index, howMany /* model1, ... modelN */) {
  158. var args = _.toArray(arguments).slice(2).concat({at: index}),
  159. removed = this.models.slice(index, index + howMany);
  160. this.remove(removed).add.apply(this, args);
  161. return removed;
  162. }
  163. // show a log of events getting fired
  164. function log_events(event, model) {
  165. var now = new Date();
  166. $("#example1_events").prepend(
  167. $("<option/>").text([
  168. ":", now.getSeconds(), ":", now.getMilliseconds(),
  169. "[" + event + "]",
  170. JSON.stringify(model)
  171. ].join(" "))
  172. )
  173. .scrollTop(0);
  174. }
  175. $("#add_car").click(function () {
  176. cars.add({make: "Tesla", model: "S", year: 2012, weight: 4647.3});
  177. })
  178. </script>
  179. </div>
  180. </div>
  181. </div>
  182. <div class="rowLayout nomargin">
  183. <div class="descLayout">
  184. <div class="pad"><p>For more examples, head back to the <a href="../index.html">main page</a>.</p>
  185. <p class="small">Handsontable &copy; 2012 Marcin Warpechowski and contributors.<br> Code and documentation
  186. licensed under the The MIT License.</p>
  187. </div>
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. </body>
  193. </html>