JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script id="script_content" type="text/template">
<ul><li><%= notification_num %></li></ul>
</script>
<script id="script_content2" type="text/template">
<div id="test">CLICK HERE</div>
</script>
<div id="content"></div>
<div id="content2"></div>
JavaScript
(function($){
window.LeftNotificationModel = Backbone.Model.extend({
defaults:{
'notification_num':12
},
initialize: function() {
},
decrementNotification: function() {
alert(this.get('notification_num'));
notification_num = this.get('notification_num');
notification_num = notification_num -1;
this.set({'notification_num': notification_num });
}
});
})(jQuery);
(function($){
window.LeftNotificationView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change:notification_num', this.render);
},
render: function() {
template= _.template($('#script_content').html());
$(this.el).html(template(this.model.toJSON()));
$('#content').html(this.el);
}
});
window.PopupView = Backbone.View.extend({
events: {
'click #test': 'decrementNotification'
},
initialize: function () {
this.model.bind('change:notification_num', this.render);
},
render: function() {
template= _.template($('#script_content2').html());
$(this.el).html(template(this.model.toJSON()));
$('#content2').html(this.el);
},
decrementNotification: function() {
this.model.decrementNotification();
}
});
})(jQuery);
$(document).ready(function() {
var model = new LeftNotificationModel();
window.LeftNotificationView = new LeftNotificationView({ model: model});
window.LeftNotificationView.render();
window.PopupView = new PopupView({ model: model});
window.PopupView.render();
});