Backbone Template

Standard fiddle

by edwardmsmith

HTML

<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>
<script src="https://raw.github.com/PaulUithol/Backbone-relational/master/backbone-relational.js"></script>

JavaScript

$(function() {
    
    Search = Backbone.RelationalModel.extend({
      urlRoot: '/searches',
    
      relations: [{
        type: Backbone.HasMany,
        key: 'services',
        relatedModel: 'Service',
        reverseRelation: {
          key: 'search'
        }
      }],
        
      initialize: function(options) {
        console.log(this, "has services:", this.get('services'));
      }
    });
    
    
    Service = Backbone.RelationalModel.extend({
      initialize: function() {
        console.log(this, "in", this.get('search'));
          this.bind('all', function(e,m,c,o) {
              console.log(e,m,c,o);
          });
      }
    });
        
    s1 = new Search({name:"Some Search",services:[{name:"service1"},{name:"service2"}]});
    console.log("The first service is named",s1.get('services').first().get('name'));
    console.log("The first service's search's name is", s1.get('services').first().get('search').get('name'));
    
});