From 14af17e1b4136164512340edfee08a63306c48b6 Mon Sep 17 00:00:00 2001 From: Is Isilon Date: Fri, 29 Jan 2016 19:52:05 +0800 Subject: [PATCH] Got localstorage working --- bower.json | 5 +- public/javascripts/app.js | 2 +- public/javascripts/collections/list.js | 11 ++- public/javascripts/data/demo_data.js | 115 ------------------------- public/javascripts/views/list.js | 33 ++++--- 5 files changed, 34 insertions(+), 132 deletions(-) delete mode 100644 public/javascripts/data/demo_data.js diff --git a/bower.json b/bower.json index f7a8bff..4857b9e 100644 --- a/bower.json +++ b/bower.json @@ -15,10 +15,13 @@ "text": "requirejs-text#~2.0.14", "jquery": "~2.2.0", "backbone": "~1.2.3", + "backbone.localStorage": "1.1.16", "lodash": "~4.0.1", "requirejs": "~2.1.22", "underscore": "~1.8.3", "modernizr": "~3.3.1", - "zepto": "~1.1.6" + "zepto": "~1.1.6", + "localforage": "~1.3.3", + "localforage-backbone": "~0.6.2" } } diff --git a/public/javascripts/app.js b/public/javascripts/app.js index c85cceb..e03d150 100644 --- a/public/javascripts/app.js +++ b/public/javascripts/app.js @@ -5,10 +5,10 @@ require.config({ jquery: '../bower_components/jquery/dist/jquery', lodash: "../bower_components/lodash/dist/lodash.min", backbone: '../bower_components/backbone/backbone', + localstorage: '../bower_components/backbone.localStorage/backbone.localStorage', modernizr: "vendor/custom.modernizr", socket: "../bower_components/socket.io-client/socket.io", text: '../bower_components/text/text', - demoData: '../javascripts/data/demo_data', }, map: { "*": { diff --git a/public/javascripts/collections/list.js b/public/javascripts/collections/list.js index 8f65eb6..0dde463 100644 --- a/public/javascripts/collections/list.js +++ b/public/javascripts/collections/list.js @@ -1,21 +1,24 @@ define( [ 'backbone', -'models/task' +'models/task', +'localstorage', ], function( Backbone, -Task +Task, +LocalStorage ) { var List = Backbone.Collection.extend({ + localStorage: new Backbone.LocalStorage("tasks"), model: Task, url: '/tasks' - + }); return List; -}); \ No newline at end of file +}); diff --git a/public/javascripts/data/demo_data.js b/public/javascripts/data/demo_data.js deleted file mode 100644 index bb8aac6..0000000 --- a/public/javascripts/data/demo_data.js +++ /dev/null @@ -1,115 +0,0 @@ - define(function (require) { - return [{ - "id": 80, - "content": "Welcome to HackFlowy!", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 81, - "content": "An open-source WorkFlowy clone", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 82, - "content": "Built using Backbone + Socket.IO", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 83, - "content": "Desyncr pulled this together in a few hours to learn Backbone", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 84, - "content": "Feel free to try it out and hack on it", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 85, - "content": "Good Luck!", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:30.858Z", - "updatedAt": "2016-01-29T05:44:30.858Z" - }, { - "id": 86, - "content": "P.S", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:40.978Z", - "updatedAt": "2016-01-29T05:44:40.978Z" - }, { - "id": 88, - "content": "It makes sense if you don't think about it; I haven't", - "parentId": 0, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:44:58.737Z", - "updatedAt": "2016-01-29T05:45:50.939Z" - }, { - "id": 89, - "content": "Make love not war", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:45:03.048Z", - "updatedAt": "2016-01-29T05:45:57.481Z" - }, { - "id": 91, - "content": "Love can be brought not sold", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:45:32.331Z", - "updatedAt": "2016-01-29T05:46:10.478Z" - }, { - "id": 93, - "content": "How do I love thee? Let me count the ways - Shakespeare", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:46:25.119Z", - "updatedAt": "2016-01-29T05:48:00.604Z" - }, { - "id": 95, - "content": "Therefore: love can be listed and lists can be loved", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:46:38.998Z", - "updatedAt": "2016-01-29T05:48:22.937Z" - }, { - "id": 96, - "content": "Conclusion: lists and love should be free", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:47:26.684Z", - "updatedAt": "2016-01-29T05:48:29.796Z" - }, { - "id": 97, - "content": "But how can our lists be real if our love isnt? - Jaden Smith", - "parentId": 88, - "isCompleted": false, - "priority": 0, - "createdAt": "2016-01-29T05:47:46.930Z", - "updatedAt": "2016-01-29T05:47:46.930Z" - }]; - }); diff --git a/public/javascripts/views/list.js b/public/javascripts/views/list.js index 957ebd7..c9de32c 100644 --- a/public/javascripts/views/list.js +++ b/public/javascripts/views/list.js @@ -3,7 +3,7 @@ define( 'backbone', 'collections/list', 'views/task', - 'demoData' + 'data/demo' ], function ( @@ -24,18 +24,29 @@ define( initialize: function () { Tasks = this.collection = new List(); - var fetchPromise = this.collection.fetch(); - - fetchPromise.fail(function (e) { - // if the server isn't running load some demo data and a demo warning - $('#header').append('
Warning: Running in demo mode, all work will be lost
'); - var data = demoData; - for (var i = 0; i < data.length; i++) { - Tasks.add(data[i]); - } - }, this); this.listenTo(this.collection, 'add', this.renderTask); + + this.collection.fetch() + .fail(function (e) { + // if the server isn't running so load some demo data and a demo warning + $('#header').append('
Warning: Running in demo mode, all work will be lost
'); + for (var i = 0; i < demoData.length; i++) { + Tasks.add(demoData[i]); + } + }) + .always(function(data){ + console.log({data:data}); + if (data.length===0){ + // if the server isn't running so load some demo data and a demo warning + $('#header').append('
Warning: Running in demo mode, work will be lost
'); + for (var i = 0; i < demoData.length; i++) { + var task = Tasks.add(demoData[i]); + task.save(); + } + } + }); + }, render: function (data) {