JSFiddle - React, Tailwind, and code Playground

by Eric Howe

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js"></script>
<div id="view-goes-here"></div>
<script id="tmpl" type="text/template">
    <%= task.name %><br><%= user.name %>
</script>
<button>populate task</button>

CSS

#view-goes-here {
    padding: 50px;
    border: 1px solid black;
}

JavaScript

var M = Backbone.Model.extend({});
var V = Backbone.View.extend({
    template: _.template($('#tmpl').html()),
    initialize: function(user, task) {
        _.bindAll(this, 'render');
        this.user = user;
        this.task = task;
        this.user.on('change', this.render);
        this.task.on('change', this.render);
    },
    render: function() {
        if(!this.user.isEmpty()
        && !this.task.isEmpty()) {
            this.$el.append(this.template({
                task: this.task.toJSON(),
                user: this.user.toJSON()
            }));
        }
        return this;
    }
});
var u = new M({ name: 'J.R. "Bob" Dobbs'});
var t = new M({});
var v = new V(u, t);
$('#view-goes-here').append(v.render().el);

$('button').click(function() {
    t.set('name', 'Slack off');
});