
function Booking() {

}

function Resource() {

}

function ResourceType(data) {
	if (data) {
		this.updateData(data);
	}

	var resourceType = this;

	this.collection(Resource, {
		sync: function(){
			http().get('/rbs/type/' + resourceType._id + '/resources').done(function(resources){
				this.load(resources);
			}.bind(this))
		},
		removeSelection: function(){
			var counter = this.selection().length;
			this.selection().forEach(function(item){
				http().delete('/rbs/resource/' + item._id).done(function(){
					counter = counter - 1;
					if (counter === 0) {
						Collection.prototype.removeSelection.call(this);
						resourceType.resources.sync();
					}
				});
			});
		},
		behaviours: 'rbs'
	});
}

ResourceType.prototype.toJSON = function() {
	return {
		name : this.name,
		validation : this.validation,
		school_id : this.school_id
	}
};

model.build = function(){
	this.makeModels([ResourceType, Resource, Booking]);

	this.collection(ResourceType, {
		sync: function(){
			var collection = this;
			// Load the ResourceTypes
			http().get('/rbs/types').done(function(resourceTypes){
				if (model.resources === undefined) {
					// Load the Resources once
					this.one('sync', function(){
						model.buildResources();
					});
				}
				this.load(resourceTypes);
			}.bind(this));
		},
		behaviours: 'rbs'
	});
};

model.buildResources = function() {
	this.collection(Resource, {
		sync: function(){
			var collection = this;
			// Load the Resources
			http().get('/rbs/resources').done(function(resources){
				collection.load(resources);
				// Load each ResourceType's collection with associated Resources
				var groupedResources  = _.groupBy(resources, function(resource) {
					return resource.type_id;
				});
				var actions = model.resourceTypes.length;
				model.resourceTypes.forEach(function(resourceType){
					resourceType.resources.all = [];
					if (_.has(groupedResources, resourceType.id)) {
						_.each(groupedResources[resourceType.id], function(res){
							resourceType.resources.all.push(res);
						});
					}
				});
				model.resourceTypes.trigger('sync');
			});
		},
		behaviours: 'rbs'
	});
	this.resources.sync();
}
@@ -1524,24 +1524,92 @@
 resources)%7B%0A
+%09%09%09%09var actions = (resources !== undefined ? resources.length : 0);%0A
 %09%09%09%09collecti
@@ -1625,19 +1625,39 @@
 esources
-);%0A
+, function(resource)%7B%0A%09
 %09%09%09%09// L
@@ -1660,20 +1660,19 @@
 // Load 
-each
+the
  Resourc
@@ -1718,18 +1718,18 @@
 urce
-s
 %0A
+%09
 %09%09%09%09var 
 grou
@@ -1728,47 +1728,53 @@
 var 
-groupedR
+r
 esource
-s 
+Type
  = _.
-groupBy(resources
+find(model.resourceTypes.all
 , fu
@@ -1785,19 +1785,13 @@
 on(r
-esource) 
+t)
 %7B%0A
+%09
 %09%09%09%09
@@ -1797,16 +1797,26 @@
 %09%09return
+ rt.id ===
  resourc
@@ -1830,24 +1830,25 @@
 id;%0A%09%09%09%09
+%09
 %7D);%0A%09%09%09%09
 var acti
@@ -1843,106 +1843,44 @@
 %09%09%09%09
-var actions = model.resourceTypes.length;%0A%09%09%09%09model.resourceTypes.forEach(function(resourceType)
+%09if (resourceType !== undefined) 
 %7B%0A
+%09
 %09%09%09%09
@@ -1910,201 +1910,76 @@
 .all
- = %5B%5D;%0A%09%09%09%09%09if (_.has(groupedResources, resourceType.id)) %7B%0A%09%09%09%09%09%09_.each(groupedResources%5BresourceType.id%5D, function(res)%7B%0A%09%09%09%09%09%09%09resourceType.resources.all.push(res);%0A%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A
+.push(resource);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09actions--;%0A%09%09%09%09%09if (actions === 0) %7B%0A%09%09
 %09%09%09%09
@@ -2015,16 +2015,31 @@
 sync');%0A
+%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A
 %09%09%09%7D);%0A%09

Fix model Resources collections and each ResourceType's collection sync