/demo/backbone.html
HTML | 219 lines | 171 code | 36 blank | 12 comment | 0 complexity | 325f3b652e518efd4a5d33ccf2932d0c MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
- <!doctype html>
- <html>
- <head>
- <meta charset='utf-8'>
- <title>Backbone.js - Handsontable</title>
- <!--
- Loading Handsontable (full distribution that includes all dependencies apart from jQuery)
- -->
- <script data-jsfiddle="common" src="../lib/jquery.min.js"></script>
- <script data-jsfiddle="common" src="../dist/jquery.handsontable.full.js"></script>
- <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/jquery.handsontable.full.css">
- <!--
- Loading demo dependencies. They are used here only to enhance the examples on this page
- -->
- <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
- <script src="js/samples.js"></script>
- <script data-jsfiddle="common" src="js/backbone/lodash.underscore.js"></script>
- <script data-jsfiddle="common" src="js/backbone/backbone.js"></script>
- <script data-jsfiddle="common" src="js/backbone/backbone-relational/backbone-relational.js"></script>
- <script src="js/highlight/highlight.pack.js"></script>
- <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
- <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
- <!--
- Facebook open graph. Don't copy this to your project :)
- -->
- <meta property="og:title" content="Integrate with Backbone.js">
- <meta property="og:description"
- content="Bind your Backbone collections to Handsontable">
- <meta property="og:url" content="http://handsontable.com/demo/backbone.html">
- <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
- <meta property="og:image:type" content="image/png">
- <meta property="og:image:width" content="409">
- <meta property="og:image:height" content="164">
- <link rel="canonical" href="http://handsontable.com/demo/backbone.html">
- <!--
- Google Analytics for GitHub Page. Don't copy this to your project :)
- -->
- <script src="js/ga.js"></script>
- </head>
- <body>
- <div class="wrapper">
- <div class="wrapper-row">
- <div id="global-menu-clone">
- <h1><a href="../index.html">Handsontable</a></h1>
- </div>
- <div id="container">
- <div class="columnLayout">
- <div class="rowLayout">
- <div class="descLayout">
- <div class="pad" data-jsfiddle="example1">
- <h2>Backbone.js</h2>
- <p>
- <a href="http://backbonejs.org/">Backbone.js</a> is a client-side
- MV* framework that can do some pretty smart things
- with data going to and coming back from a server, and has a great
- event model for keeping multiple views in sync.
- </p>
- <p>
- This little example shows how Backbone Models and Collections can
- work with Handsontable. Below, you'll see events firing from
- changes in the <code>CarCollection</code> by Handsontable or
- otherwise.
- </p>
- <div id="example1"></div>
- <p>
- <button name="dump" data-dump="#example1" title="Prints current data source to Firebug/Chrome Dev Tools">
- Dump data to console
- </button>
- <button id="add_car">Simulate a new Car</button>
- <br>
- <select multiple="multiple" id="example1_events" style="width: 100%; height: 100px"></select>
- </p>
- <p>
- <strong>Please note</strong> that Backbone integration is a work in progress since Handsontable 0.8.14.
- The code presented here has 2 known issues:
- </p>
- <ul>
- <li>inserting and removing rows or columns triggers errors, both when using <code>alter</code> method and
- the context menu
- </li>
- <li><code>minSpareRows</code> does not have effect directly after row was added from Backbone (as a
- workaround, you would need to call <code>loadData</code> instead of <code>render</code>)
- </li>
- </ul>
- <p>
- Both issues will be addressed in future versions of HT. Contributions are welcome!
- </p>
- </div>
- </div>
- <div class="codeLayout">
- <div class="pad">
- <div class="jsFiddle">
- <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
- </div>
- <script data-jsfiddle="example1">
- var CarModel = Backbone.Model.extend({});
- var CarCollection = Backbone.Collection.extend({
- model: CarModel,
- // Backbone.Collection doesn't support `splice`, yet! Easy to add.
- splice: hacked_splice
- });
- var cars = new CarCollection();
- // since we're not using a server... make up some data. This will make
- // a couple CarModels from these plain old objects
- cars.add([
- {make: "Dodge", model: "Ram", year: 2012, weight: 6811},
- {make: "Toyota", model: "Camry", year: 2012, weight: 3190},
- {make: "Smart", model: "Fortwo", year: 2012, weight: 1808}
- ]);
- var $container = $("#example1");
- $container.handsontable({
- data: cars,
- dataSchema: makeCar,
- contextMenu: true,
- columns: [
- attr("make"),
- attr("model"),
- attr("year")
- ],
- colHeaders: ["Make", "Model", "Year"]
- //minSpareRows: 1 //see notes on the left for `minSpareRows`
- });
- // this will log all the Backbone events getting fired!
- cars.on("all", log_events)
- .on("add", function () {
- $container.handsontable("render");
- })
- .on("remove", function () {
- $container.handsontable("render");
- });
- // you'll have to make something like these until there is a better
- // way to use the string notation, i.e. "bb:make"!
- // normally, you'd get these from the server with .fetch()
- function attr(attr) {
- // this lets us remember `attr` for when when it is get/set
- return {data: function (car, value) {
- if (_.isUndefined(value)) {
- return car.get(attr);
- }
- car.set(attr, value);
- }};
- }
- // just setting `dataSchema: CarModel` would be great, but it is non-
- // trivial to detect constructors...
- function makeCar() {
- return new CarModel();
- }
- // use the "good" Collection methods to emulate Array.splice
- function hacked_splice(index, howMany /* model1, ... modelN */) {
- var args = _.toArray(arguments).slice(2).concat({at: index}),
- removed = this.models.slice(index, index + howMany);
- this.remove(removed).add.apply(this, args);
- return removed;
- }
- // show a log of events getting fired
- function log_events(event, model) {
- var now = new Date();
- $("#example1_events").prepend(
- $("<option/>").text([
- ":", now.getSeconds(), ":", now.getMilliseconds(),
- "[" + event + "]",
- JSON.stringify(model)
- ].join(" "))
- )
- .scrollTop(0);
- }
- $("#add_car").click(function () {
- cars.add({make: "Tesla", model: "S", year: 2012, weight: 4647.3});
- })
- </script>
- </div>
- </div>
- </div>
- <div class="footer-text">Handsontable © 2012 Marcin Warpechowski and contributors.<br> Code and documentation
- licensed under the The MIT License.
- </div>
- </div>
- </div>
- </div>
- </div>
- <div id="outside-links-wrapper"></div>
- </body>
- </html>