Dionysus.module('Entities', function(Entities, Dionysus, Backbone, Marionette, $) {
  'use strict';

  var Course = Backbone.RelationalHalResource.extend({
    url: function(){
      return this.id ? '/api/v1/courses/' + this.id : '/api/v1/courses';
    },
    isNew: function(){
      return this.id == null || this.id == undefined;
    },
    initialize: function(options){
      if(options && options.id){
        this.id = options.id;
      }
    },
    getCategory: function(){
      var category = new Backbone.Model();
      category.url = this.link('category').get('href');
      var defer = $.Deferred();
      category.fetch().then(function(){
        defer.resolve(category);
      });
      return defer.promise(category);
    },
    getConsultant: function(){
      var consultant = new Backbone.Model();
      consultant.url = this.link('consultant').get('href');
      var defer = $.Deferred();
      consultant.fetch().then(function(){
        defer.resolve(consultant);
      });
      return defer.promise(consultant);
    }
  });

  var CourseCollection = Backbone.RelationalHalResource.extend({
    url: '/api/v1/courses',
    halEmbedded: {
      courses: {
        type: Backbone.HasMany,
        relatedModel: Course
      }
    }
  });

  var CourseCategory = Backbone.RelationalHalResource.extend({
    url: '/api/v1/courseCategories'

  });

  var CourseCategoryCollection = Backbone.RelationalHalResource.extend({
    url: '/api/v1/courseCategories',
    halEmbedded: {
      courseCategories: {
        type: Backbone.HasMany,
        relatedModel: CourseCategory
      }
    },
    toSelection : function() {
      var categories = this.embedded('courseCategories');
      return categories.map(function(category) {
        return {
          name : category.get('name'),
          link : category.link('self').href()
        }
      });
    }
  });

  Dionysus.reqres.setHandler('course:entity', function(id) {
    var course = new Course({id:id});
    var defer = $.Deferred();
    course.fetch().then(function() {
      defer.resolve(course);
    });
    return defer.promise();
  });

  Dionysus.reqres.setHandler('course:entities', function(id) {
    var courses = new CourseCollection();
    var defer = $.Deferred();
    courses.fetch().then(function() {
      defer.resolve(courses);
    });
    return defer.promise();
  });

  Dionysus.reqres.setHandler('course:new', function() {
    return new Course();
  });

  Dionysus.reqres.setHandler('course:category', function(id) {
    var category = new CourseCategory(id);
    var defer = $.Deferred();
    category.fetch().then(function() {
      defer.resolve(category);
    });
    return defer.promise();
  });

  Dionysus.reqres.setHandler('course:categories', function() {
    var resources = new CourseCategoryCollection(), defer = $.Deferred();
    resources.fetch().then(function() {
      defer.resolve(resources);
    });
    return defer.promise();
  });

  Dionysus.reqres.setHandler('course:bookedby', function(userid) {
    var courses = new Entities.UserCourseCollection({appendUrl:'/' + userid + '/courses'});
    var defer = $.Deferred();
    courses.fetch().then(function() {
      defer.resolve(courses);
    });
    return defer.promise();
  });
});


@@ -1926,32 +1926,33 @@
  function(id) %7B%0A
+%0A
     var course =
@@ -1951,16 +1951,77 @@
 course =
+ Course.find(%7Bid:id%7D);%0A    if(course == null)%7B%0A      course =
  new Cou
@@ -2030,32 +2030,34 @@
 e(%7Bid:id%7D);%0A    
+ 
+ 
 var defer = $.De
@@ -2066,24 +2066,27 @@
 rred();%0A    
+  
+ 
 course.fetch
@@ -2104,32 +2104,33 @@
 ction() %7B%0A      
+ 
 defer.resolve(co
@@ -2136,28 +2136,34 @@
 ourse);%0A    
+ 
+  
 %7D);%0A
+   
     return d
@@ -2170,32 +2170,57 @@
 efer.promise();%0A
+    %7D%0A    return course;%0A
   %7D);%0A%0A  Dionysu

fix for defect can not edit course from course list view