Skip to content

Commit

Permalink
#20 Replacing js in basic example to use plain addEventListener(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Oct 9, 2012
1 parent e5d7f07 commit 1f9cee7
Showing 1 changed file with 22 additions and 92 deletions.
114 changes: 22 additions & 92 deletions examples/basic/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,31 @@
<script src="/client.js"></script>

<script>
var Message = Backbone.Model.extend({

initialize: function() {
this.bind('error', function(model, err) {
alert(err.message);
});
}

});

var Messages = Backbone.Collection.extend({

// Specify the backend with which to sync
backend: 'messages',

model: Message,

initialize: function() {
// Setup default backend bindings
// (see lib/browser.js for details).
this.bindBackend();
}

});

var MessageView = Backbone.View.extend({

tagName: 'li',

events: {
'click .delete': 'delete'
},

initialize: function() {
_.bindAll(this, 'render', 'delete');

this.template = _.template($('#message-template').html());
},

render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},

delete: function(e) {
e.preventDefault();
this.model.destroy();
}

});
var Message = function(){

return {
data: "",
backend: "backend"
}
};

var MessagesView = Backbone.View.extend({

events: {
'click .send': 'send',
'keypress .message': 'keypress'
},

initialize: function(options) {
_.bindAll(this, 'render', 'send', 'keypress');

this.collection.bind('add', this.render);
this.collection.bind('change', this.render);
this.collection.bind('remove', this.render);
this.collection.bind('reset', this.render);
this.collection.fetch();

this.template = _.template($('#messages-template').html());
},

render: function() {
$(this.el).html(this.template());

this.collection.each(function(message) {
var view = new MessageView({ model: message });
this.$('ul').append(view.render().el);
});

return this;
},

send: function() {
this.collection.create({ text: this.$('.message').val() });
this.$('.message').val('');
},

keypress: function(e) {
if (e.which === 13) this.send();
}

});
function setupEvents(element){

element.addEventListener('{{name}}:create', function(e) {
var data = e.response;
}, false);
element.addEventListener('{{name}}:update', function(e) {
var data = e.response;
}, false);
element.addEventListener('{{name}}:delete', function(e) {
var data = e.response;
}, false);

}

$(function() {
window.messages = new Messages();
new MessagesView({ el: $('#messages'), collection: messages }).render();
var message = new Message();
setupEvents(message);
});
</script>

Expand Down

0 comments on commit 1f9cee7

Please sign in to comment.