/sites/web/bower_components/localforage-backbone/examples/simple.html
https://bitbucket.org/aswinvk28/smartpan-stock-drupal · HTML · 82 lines · 73 code · 9 blank · 0 comment · 0 complexity · e7c18cd5cbc390e72caf256e72699157 MD5 · raw file
- <!doctype html>
- <html>
- <head>
- <meta charset="utf8" />
- <title>Simple Backbone.localForage example</title>
- <script src="../bower_components/jquery/dist/jquery.js"></script>
- <script src="../bower_components/underscore/underscore.js"></script>
- <script src="../bower_components/backbone/backbone.js"></script>
- <script src="../bower_components/localforage/dist/localforage.js"></script>
- <script src="../dist/localforage.backbone.js"></script>
- </head>
- <body>
- <script type="template/form" id="formtpl">
- <form>
- <input type="text" name="content" />
- <input type="submit" value="Create" />
- </form>
- </script>
- <script>
- (function (Backbone, _) {
- 'use strict';
- // Set driver (optional, but we use localforage here so developers
- // can more easily inspect it).
- localforage.setDriver('localStorageWrapper');
- // We store offline data inside a collection. This is how we tell
- // a model/collection to store data offline with localForage.
- var MyCollection = Backbone.Collection.extend({
- sync: Backbone.localforage.sync('MyCollection'),
- model: Backbone.Model.extend({
- sync: Backbone.localforage.sync('ModelNamespace')
- })
- });
- // A view with a form and the collection contents
- var MyFormView = Backbone.View.extend({
- events: {
- 'submit form': 'handleSaveModel'
- },
- initialize: function (options) {
- // For the sake of the example, this is very bad
- // performance-wise :)
- this.listenTo(this.collection, 'add remove change sync', this.render);
- },
- createModel: function (model) {
- return $("<div>", {
- 'class': 'saved-data',
- text: model.get('content')
- });
- },
- handleSaveModel: function (event) {
- event.preventDefault();
- // It'll write on the localforage offline store
- this.collection.create({content: this.$('[name="content"]').val()});
- },
- render: function () {
- // Render the form template on this.$el and append the
- // collection content
- this.$el
- .html(_.template($('#formtpl').html()))
- .append(this.collection.map(this.createModel));
- }
- });
- // Instancing the collection and the view
- var collectionInstance = new MyCollection();
- var myFormView = new MyFormView({
- el: $('<div>', {'class': 'content'}).appendTo(document.body),
- collection: collectionInstance
- });
- myFormView.render();
- collectionInstance.fetch();
- }(this.Backbone, this._));
- </script>
- </body>
- </html>