Use socketio for syncing task changes across clients

This commit is contained in:
Abhishek Das
2013-03-30 18:10:12 +05:30
parent 0485272d93
commit 4bb398353e
502 changed files with 206510 additions and 64 deletions
+1 -9
View File
@@ -2,13 +2,5 @@ var app = app || {};
var ENTER_KEY = 13;
$(function(){
var tasks = [
{content: 'helloworld'},
{content: 'wtf'},
{content: 'rotterdam'},
{content: 'hahahah'}
];
new app.ListView(tasks);
new app.ListView();
});
+2 -1
View File
@@ -4,7 +4,8 @@ var app = app || {};
app.List = Backbone.Collection.extend({
model: app.Task
model: app.Task,
url: '/tasks'
});
-2
View File
@@ -9,8 +9,6 @@ var app = app || {};
content: ''
},
url: '/task'
});
}());
+2
View File
File diff suppressed because one or more lines are too long
+18 -3
View File
@@ -5,15 +5,30 @@ var app = app || {};
app.ListView = Backbone.View.extend({
el: $("#main .children"),
initialize: function(initialTasks) {
this.collection = new app.List(initialTasks);
this.render();
events: {
'click #add': 'addTask'
},
initialize: function() {
this.collection = new app.List();
this.collection.fetch();
this.render();
this.listenTo(this.collection, 'add', this.renderTask);
},
addTask: function() {
console.log('yes');
//var view = new app.TaskView({model: task})
},
render: function() {
this.collection.each(function(task) {
this.renderTask(task);
}, this);
},
renderTask: function(task) {
var taskView = new app.TaskView({
model: task
+28 -12
View File
@@ -9,35 +9,51 @@ var app = app || {};
events: {
'click .task': 'edit',
'blur .edit': 'close',
'keypress .edit': 'add'
'blur .edit': 'close'
},
initialize: function() {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
},
render: function() {
var tmpl = _.template(this.template);
var task = this;
this.$el.html(tmpl(this.model.toJSON()));
this.$input = this.$('.edit');
socket.on('task', function(data){
if (task.model.id == data.id) {
if (task.$input.val != data.content)
task.$input.val(data.content);
}
});
return this;
},
edit: function() {
this.$el.addClass('editing');
this.$input.focus();
var id = (this.model.id === undefined) ? '' : this.model.id;
var parent_id = (this.model.parent_id === undefined) ? '' : this.model.parent_id;
this.$input.on('keyup',function(){
var value = $(this).val().trim();
socket.emit('task', {
id: id,
parent_id: parent_id,
content: value
});
});
},
close: function() {
var value = this.$input.val().trim();
this.model.save({content: value});
this.$el.removeClass('editing');
},
add: function(e) {
if (e.which === ENTER_KEY) {
this.$input.blur();
var render = new app.TaskView({model: new app.Task()}).render();
render.$el.insertAfter(this.$el);
render.$input.focus();
if (value === '') {
this.model.destroy();
}
else
this.model.save({content: value});
this.$el.removeClass('editing');
}
});