PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/backbone.html

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