Simple Backbone.ModelBinding Example
shows how simple modelbinding can be with the Backbone.ModelBinding plugin from http://github.com/derickbailey/backbone.modelbinding
HTML
<script src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone.js"></script>
<div id="person">
First Name: <input id="firstName" value=""><br>
Last Name: <input id="lastName" value=""><br>
Full Name: <span id="fullName"></span>
</div>
JavaScript
Person = Backbone.Model.extend({});
PersonView = Backbone.View.extend({
el: '#person',
events: {
'change': 'change',
},
initialize: function(){
this.listenTo(this.model, 'change', this.render);
this.render();
},
change: function(){
var firstName = $('#firstName').val();
var lastName = $('#lastName').val();
this.model.set({firstName: firstName, lastName: lastName});
},
render: function(){
this.$('#firstName').val(this.model.get('firstName'));
this.$('#lastName').val(this.model.get('lastName'));
var fullName = this.model.get('firstName')
+ ' ' + this.model.get('lastName');
this.$('#fullName').text(fullName);
},
});
person = new Person({lastName: "Yamada", firstName: "Taro"});
personView = new PersonView({model: person});
person.set('lastName','dsdd')