Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Backbone does support <strong>implementing</strong> model binding, but it's not <strong>automatic</strong> as in some other client-side application frameworks, for example, <a href="http://angularjs.org" rel="nofollow">AngularJS</a>'s data-binding through <code>ng-controller</code> or <a href="http://emberjs.com/documentation/#toc_binding-element-attributes-with-bindattr" rel="nofollow">EmberJS</a>'s <code>bindAttr</code>.</p> <p>For some really rudimentary code to update views as a model's state changes, use <code>Backbone.View</code>'s <a href="http://backbonejs.org/#View-delegateEvents" rel="nofollow"><code>events</code> object</a>, as in this example:</p> <pre><code>var ExampleView = Backbone.View.extend({ id: 'myView', events: { // using an #id selector here is also acceptable 'change [data-attrname=name]': function(e) { // update the value in the model this.model.set('name', $(e.target).val()); } }, initialize: function(options) { this.model.on('change', this.render, this); }, render: function() { var html = _.template('&lt;h1 data-attrname="name"&gt;&lt;%= name %&gt;&lt;/h1&gt;', this.model.toJSON()); this.$el.html(html); return this; } }); </code></pre> <p>Which will render the following HTML (<code>myView</code> is the div implicitly created by ExampleView):</p> <pre><code>&lt;div id="myView"&gt; &lt;h1 data-attrname="name"&gt;&lt;/h1&gt; &lt;/div&gt; </code></pre> <p>This is essentially doing binding manually from the UI and wholesale (re-<code>render</code>ing the entire view) when the model changes. Backbone is intentionally designed to be less opinionated than some other frameworks, to allow you to implement things as you see fit with the addition of other libraries.</p> <p>For some more sophisticated techniques on how to model bind more automatically, here are a few resources listed by their authors:</p> <ol> <li><a href="https://github.com/derickbailey/backbone.modelbinding" rel="nofollow">Los Techies / Derek Bailey</a></li> <li><a href="https://github.com/theironcook/Backbone.ModelBinder" rel="nofollow">theironcook</a></li> <li><a href="http://xtargets.com/2011/06/11/binding-model-attributes-to-form-elements-with-backbone-js/" rel="nofollow">XTargets</a></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload