ko.toJSON($root,null,2) return error-Collection of common programming errors

I’m trying to debug ko (version: 2.2.1) using

 tag:
 

but I'm getting this:
Uncaught Error: Unable to parse bindings.
Message: TypeError: Object object has no method 'getType';
Bindings value: text:ko.toJSON($root,null,2) 

UPDATE:
It seems like the $root object is actually the windows object, why is it happening, how can i make sure it remains in the scope of my ViewModel?
The scenario is like this:
I have a view that contains also a popup template. When a user clicks a button the popup shows and that is when I get this error (the $root looses the ViewModel scope and becomes the window object).
VIEW:
        Done 
        
        //THIS OPENS THE PROBLEMATIC POPUP
        
        

        
        
        

        
            

VIEWMODEL:
  /**
 * The module for the common Entities ViewModel
 */
define(['ko', 'komap', 'kopost', 'ca', 'sp/utils', 'sp/db'], function (ko, komap, kopost, ca, util, db) {

    /**
     * The Entities ViewModel.  This VM just exposes a collection of entities in a single property.
     * The model to retrieve is obtained from the last portion of the URL path.
     */

    function EntitiesViewModel(ctx, data) {
        this.init(ctx,data);

    }

    // EntitiesViewModel.prototype = new ca.ViewModel({});

    // EntitiesViewModel.prototype.constructor = EntitiesViewModel;

    ko.utils.extend(EntitiesViewModel.prototype,(function(){



        init = function(ctx,data){
            var self = this;
            strategyId = ctx.param.st;
            entitySet = db.getEntitySetFromElementType(ctx.param.entity);
                parentEntitySet = _getParentEntitySet(entitySet);                      
                childPropName = util.getChildEntityPropertyName(parentEntitySet);

            self.editMode = ko.observable(false);
            self.entities = ko.observableArray([]);
            self.parentChildArr = ko.observableArray([]);   
            self.users = ko.observableArray([]);

            self.parentPropName = util.getParentEntityPropertyName(entitySet);
            self.parentTypeName = util.getParentTypeName(entitySet);    
            self.parentTypeNamePlural = util.getPluralName(self.parentTypeName);

            self.title = ko.computed(function () {
                // temporary method to get the plural name.
                return util.getPluralName(entitySet.collectionName);
            });

            self.addItem = ko.observable(entitySet.addNew());
            self.addItem().name = ko.observable("");
            self.addItem().shortDescription = ko.observable("");
            self.addItem().mission = ko.observable("");
            self.addItem().period = ko.observable("");
            self.addItem().ownedBy = ko.observable("");
            self.addItem().assignedTo = ko.observable("");

            _loadData(self);
        },



        bindPopup = function (o, e) {
            openPopup(o, e);
            selectedEntity = o;
            var popupId = $(e.target).attr('href');
            ko.applyBindings(self, $('#overlay').find(popupId).get(0));
            db.User.toArray( this.users );
        },



        startEdit = function (o, e) {
            self.editMode(true);
            e.stopPropagation();
        },

        cancel = function (o, e) {  
            closePopup(o, e);
            this.addItem (entitySet.addNew());
        },




        saveEntity = function (o, e) {
            var self = this;
            var parent = parentEntitySet.addNew({ id: strategyId });
            parentEntitySet.attachOrGet(parent);

            var entity = {
                name: self.addItem().name,
                shortDescription: self.addItem().shortDescription,
                description: self.addItem().description
            }

            entity[self.parentPropName] = parent;

            var newEntity = entitySet.addNew(entity);
            entitySet.attachOrGet(newEntity);
            entitySet.add(newEntity);

            db.saveChanges().done(function (i) {
                closePopup(o, e);                               
                _loadData(self);
                self.addItem (entitySet.addNew());
                kopost.publish("ca.sp.entitiesChanged", "Add new Entity");

            }).fail(function (error) {
                self.addItem (entitySet.addNew());
                console.log('Error = ' + error);

            });

            //detach the entities after save to avoid having them saved again
            entitySet.detach(newEntity);
            parentEntitySet.detach(parent);

            e.stopPropagation();
        };


        return {
            init: init,
            setEditMode: setEditMode,
            bindPopup: bindPopup,
            cancelDelete: cancelDelete,
            startEdit: startEdit,
            cancel: cancel,
            deleteEntity: deleteEntity,
            saveEntity: saveEntity
        }

    }()));
    return EntitiesViewModel;
});

        

        
            
            
            

            
            
  • :
                                
    
            
            
    
    //HERE IS THE PROBLEM - '$root' becomes the windows object when i open this popup
     
                 
    
                    Done
                    Cancel
                
                
                
                 

    Add New

    
             
             
                
                    Title
                    
                
                
                    Overview
                    
                
                
                    Mission
                    
                
                
                    Period
                    
                
                
                    Owned By
                    
                
                
                    Assigned to