JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<div class="pig">
<h1>Harry</h1>
<input type="text" value="Harry" />
</div>
<div class="pig">
<h1>Jill</h1>
<input type="text" value="Jill" />
</div>
<div class="pig">
<h1>Bob</h1>
<input type="text" value="Bob" />
</div>
JavaScript
$(function() {
var PigModel = Backbone.Model.extend()
var PigView = Backbone.View.extend({
events: {
'change input': function(e) {
this.model.set('name', e.currentTarget.value)
this.render()
}
},
render: function() {
this.$el.html(
'<h1>' + this.model.get('name') + '</h1>' +
'<input type="text" value="' + this.model.get('name') + '" />'
)
}
})
var pig_data = [
{"name": "Harry"},
{"name": "Jill"},
{"name": "Bob"}
]
// the magic starts here
var pig_number = 0
$('.pig').each(function() {
new PigView({
el: this,
model: new PigModel(pig_data[pig_number])
})
})
})