// Backbone.ViewModel v0.1.0
//
// Copyright (C)2012 Tom Hallett
// Distributed Under MIT License
//
// Documentation and Full License Available at:
// http://github.com/tommyh/backbone-view-model

Backbone.ViewModel = (function(Backbone, _, undefined){
  'use strict';

  var Model = Backbone.Model,
    ViewModel = function(attributes, options) {
      Model.apply(this, [attributes, options]);
      this.initializeViewModel();
    };

  _.extend(ViewModel.prototype, Model.prototype, {

    initializeViewModel: function(){
      this.set(this.get("source_models"));
      this.source_models = _.union(_.values(this.get('source_models')), (this.get('source_model') || []));
      this.setComputedAttributes();
      this.bindToChangesInSourceModel();
    },

    setComputedAttributes: function(){
      _.each(this.computed_attributes, function(value, key){
        var val = value.call(this);
        this.set(key, val);
      }, this);
    },

    bindToChangesInSourceModel: function(){
      _.each(this.source_models, function(model) {
          model.on("change", this.setComputedAttributes, this);
          model.on("add", this.setComputedAttributes, this);
          model.on("remove", this.setComputedAttributes, this);
          model.on("reset", this.setComputedAttributes, this);
      }, this);
    }

  });

  ViewModel.extend = Model.extend;

  return ViewModel;
})(Backbone, _);

Backbone.ViewCollection = (function(Backbone, _, undefined) {
    'use strict';

    var Model = Backbone.Model;
    var Collection = Backbone.Collection;

    var ViewCollection = function(options) {
        Collection.apply(this, [[], options]);
        this.sources = options.sources;
        this.initializeViewCollection();
        this.trackSort = !!options.trackSort;
        this.name = (options.name || "Unnamed ViewCollection");
        this.options = options;
    };

    _.extend(ViewCollection.prototype, Collection.prototype, {
        initializeViewCollection: function() {
            this.setComputedAttributes();
            this.bindToChangesInSources();
        },

        setComputedAttributes: function() {
            this.set(this.computeModelArray());
        },

        bindToChangesInSources: function(){
            _.each(this.sources, function(collection) {
                this.listenTo(collection, "add", this.setComputedAttributes, this);
                this.listenTo(collection, "remove", this.setComputedAttributes, this);
                this.listenTo(collection, "reset", this.setComputedAttributes, this);
                this.listenTo(collection, "change", this.setComputedAttributes, this);
                this.listenTo(collection, "destroy", this.setComputedAttributes, this);
                if (this.trackSort) {
                    this.listenTo(collection, "sort", this.setComputedAttributes, this);
                }
            }, this);
        }
    });

    ViewCollection.extend = Collection.extend;

    return ViewCollection;
})(Backbone, _);

@@ -247,16 +247,18 @@
 fined)%7B%0A
+  
   'use s
@@ -266,16 +266,18 @@
 rict';%0A%0A
+  
   var Mo
@@ -300,13 +300,17 @@
 odel
-,%0A   
+;%0A    var
  Vie
@@ -317,16 +317,52 @@
 wModel =
+ Model.extend(%7B%0A        constructor:
  functio
@@ -382,24 +382,30 @@
  options) %7B%0A
+      
       Model.
@@ -442,24 +442,30 @@
 ns%5D);%0A      
+   
+   
 this.initial
@@ -488,64 +488,19 @@
     
-%7D;%0A%0A  _.extend(ViewModel.prototype, Model.prototype, %7B%0A%0A
+    %7D,%0A    
     
@@ -528,24 +528,30 @@
 function()%7B%0A
+      
       this.s
@@ -577,24 +577,30 @@
 _models%22));%0A
+      
       this.s
@@ -622,16 +622,33 @@
 _.union(
+%0A                
 _.values
@@ -675,16 +675,32 @@
 dels')),
+%0A               
  (this.g
@@ -723,24 +723,31 @@
 ') %7C%7C %5B%5D));%0A
+%0A      
       this.s
@@ -766,24 +766,30 @@
 tributes();%0A
+      
       this.b
@@ -817,28 +817,36 @@
 odel();%0A    
+   
+ 
 %7D,%0A%0A
+    
     setCompu
@@ -870,32 +870,38 @@
 nction()%7B%0A      
+    
+  
 _.each(this.comp
@@ -939,16 +939,24 @@
 , key)%7B%0A
+        
         
@@ -979,24 +979,32 @@
 call(this);%0A
+        
         this
@@ -1019,16 +1019,22 @@
 , val);%0A
+      
       %7D,
@@ -1041,28 +1041,36 @@
  this);%0A    
+ 
+   
 %7D,%0A%0A
+    
     bindToCh
@@ -1103,24 +1103,30 @@
 on()%7B%0A      
+    
+  
 _.each(this.
@@ -1154,24 +1154,30 @@
 on(model) %7B%0A
+      
           mo
@@ -1230,32 +1230,38 @@
 his);%0A          
+ 
+     
 model.on(%22add%22, 
@@ -1297,32 +1297,38 @@
 his);%0A          
+    
+  
 model.on(%22remove
@@ -1367,32 +1367,38 @@
 his);%0A          
+      
 model.on(%22reset%22
@@ -1436,24 +1436,30 @@
 his);%0A      
+ 
+     
 %7D, this);%0A  
@@ -1464,54 +1464,26 @@
     
-%7D%0A%0A  %7D);%0A%0A  ViewModel.extend = Model.extend
+    %7D%0A%0A    %7D)
 ;%0A%0A
+  
   re
@@ -1808,49 +1808,8 @@
 es;%0A
-        this.initializeViewCollection();%0A
     
@@ -1946,16 +1946,57 @@
 ptions;%0A
+        this.initializeViewCollection();%0A
     %7D;%0A%0A
@@ -2254,33 +2254,34 @@
             
-this.set(
+var cma = 
 this.compute
@@ -2292,16 +2292,42 @@
 lArray()
+;%0A            this.set(cma
 );%0A     
@@ -2429,16 +2429,21 @@
 llection
+, key
 ) %7B%0A    

Fix ViewModel to correctly inherit from Backbone.Model