Got localstorage working

This commit is contained in:
2016-01-29 19:52:05 +08:00
parent 2996b174b8
commit 14af17e1b4
5 changed files with 34 additions and 132 deletions
+4 -1
View File
@@ -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"
}
}
+1 -1
View File
@@ -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: {
"*": {
+7 -4
View File
@@ -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;
});
});
-115
View File
@@ -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"
}];
});
+22 -11
View File
@@ -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('<div class="alert-box secondary round">Warning: Running in demo mode, all work will be lost</div>');
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('<div class="alert-box secondary round">Warning: Running in demo mode, all work will be lost</div>');
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('<div class="alert-box secondary round">Warning: Running in demo mode, work will be lost</div>');
for (var i = 0; i < demoData.length; i++) {
var task = Tasks.add(demoData[i]);
task.save();
}
}
});
},
render: function (data) {