Added static demo mode, for gh-pages demo

This commit is contained in:
2016-01-29 10:54:13 +08:00
parent 060987f6c5
commit 8b1eafaade
3 changed files with 57 additions and 42 deletions
+49 -39
View File
@@ -1,50 +1,60 @@
define(
['jquery',
'backbone',
'collections/list',
'views/task'
],
['jquery',
'backbone',
'collections/list',
'views/task'
],
function(
$,
Backbone,
List,
TaskView
) {
function (
$,
Backbone,
List,
TaskView
) {
var ListView = Backbone.View.extend({
var ListView = Backbone.View.extend({
el: $("#main .children"),
el: $("#main .children"),
events: {
'click #add': 'addTask'
},
events: {
'click #add': 'addTask'
},
initialize: function() {
Tasks = this.collection = new List();
this.collection.fetch();
this.listenTo(this.collection, 'add', this.renderTask);
},
initialize: function () {
Tasks = this.collection = new List();
var fetchPromise = this.collection.fetch();
render: function() {
this.collection.each(function(task) {
this.renderTask(task);
}, this);
},
fetchPromise.fail(function (e) {
// if the server isn't running load some demo data and a demo warning
$('#header').append('<div class="alert-box warning round">Warning: Running in demo mode, all work will be lost</div>');
var data = JSON.parse(demoData);
for (var i = 0; i < data.length; i++) {
Tasks.add(data[i]);
}
}, this);
renderTask: function(task) {
var taskView = new TaskView({
model: task
});
var a = taskView.render();
if (a.model.get('parentId')===0)
this.$el.append(a.el);
else
a.$el.insertAfter($('*[data-id="'+a.model.get('parentId')+'"]').parents('li:first'));
}
this.listenTo(this.collection, 'add', this.renderTask);
},
});
render: function () {
this.collection.each(function (task) {
this.renderTask(task);
}, this);
},
return ListView;
renderTask: function (task) {
var taskView = new TaskView({
model: task
});
var a = taskView.render();
if (a.model.get('parentId') === 0)
this.$el.append(a.el);
else
a.$el.insertAfter($('*[data-id="' + (a.model.get('parentId')||0) + '"]').parents('li:first'));
}
});
});
return ListView;
});