{"id":1937,"date":"2022-08-30T15:20:41","date_gmt":"2022-08-30T15:20:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/ko-tojsonrootnull2-return-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:41","modified_gmt":"2022-08-30T15:20:41","slug":"ko-tojsonrootnull2-return-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/ko-tojsonrootnull2-return-error-collection-of-common-programming-errors\/","title":{"rendered":"ko.toJSON($root,null,2) return error-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to debug ko (version: 2.2.1) using<\/p>\n<pre> tag:\n<\/pre>\n<pre><code> <\/code><\/pre>\n<pre>\n<br \/>but I'm getting this:\n<\/pre>\n<pre><code><code>Uncaught Error: Unable to parse bindings.\nMessage: TypeError: Object object has no method 'getType';\nBindings value: text:ko.toJSON($root,null,2) \n<\/code><\/code><\/pre>\n<pre>\n<br \/><strong>UPDATE:<\/strong>\n<br \/>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?\n<br \/>The scenario is like this:\n<br \/>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).\n<br \/><strong>VIEW:<\/strong>\n<\/pre>\n<pre><code>        Done \n        \n        \/\/THIS OPENS THE PROBLEMATIC POPUP\n        \n        \n\n        \n        \n        \n\n        \n            <\/code><\/pre>\n<pre>\n<br \/><strong>VIEWMODEL:<\/strong>\n<\/pre>\n<pre><code><code>  \/**\n * The module for the common Entities ViewModel\n *\/\ndefine(['ko', 'komap', 'kopost', 'ca', 'sp\/utils', 'sp\/db'], function (ko, komap, kopost, ca, util, db) {\n\n    \/**\n     * The Entities ViewModel.  This VM just exposes a collection of entities in a single property.\n     * The model to retrieve is obtained from the last portion of the URL path.\n     *\/\n\n    function EntitiesViewModel(ctx, data) {\n        this.init(ctx,data);\n\n    }\n\n    \/\/ EntitiesViewModel.prototype = new ca.ViewModel({});\n\n    \/\/ EntitiesViewModel.prototype.constructor = EntitiesViewModel;\n\n    ko.utils.extend(EntitiesViewModel.prototype,(function(){\n\n\n\n        init = function(ctx,data){\n            var self = this;\n            strategyId = ctx.param.st;\n            entitySet = db.getEntitySetFromElementType(ctx.param.entity);\n                parentEntitySet = _getParentEntitySet(entitySet);                      \n                childPropName = util.getChildEntityPropertyName(parentEntitySet);\n\n            self.editMode = ko.observable(false);\n            self.entities = ko.observableArray([]);\n            self.parentChildArr = ko.observableArray([]);   \n            self.users = ko.observableArray([]);\n\n            self.parentPropName = util.getParentEntityPropertyName(entitySet);\n            self.parentTypeName = util.getParentTypeName(entitySet);    \n            self.parentTypeNamePlural = util.getPluralName(self.parentTypeName);\n\n            self.title = ko.computed(function () {\n                \/\/ temporary method to get the plural name.\n                return util.getPluralName(entitySet.collectionName);\n            });\n\n            self.addItem = ko.observable(entitySet.addNew());\n            self.addItem().name = ko.observable(\"\");\n            self.addItem().shortDescription = ko.observable(\"\");\n            self.addItem().mission = ko.observable(\"\");\n            self.addItem().period = ko.observable(\"\");\n            self.addItem().ownedBy = ko.observable(\"\");\n            self.addItem().assignedTo = ko.observable(\"\");\n\n            _loadData(self);\n        },\n\n\n\n        bindPopup = function (o, e) {\n            openPopup(o, e);\n            selectedEntity = o;\n            var popupId = $(e.target).attr('href');\n            ko.applyBindings(self, $('#overlay').find(popupId).get(0));\n            db.User.toArray( this.users );\n        },\n\n\n\n        startEdit = function (o, e) {\n            self.editMode(true);\n            e.stopPropagation();\n        },\n\n        cancel = function (o, e) {  \n            closePopup(o, e);\n            this.addItem (entitySet.addNew());\n        },\n\n\n\n\n        saveEntity = function (o, e) {\n            var self = this;\n            var parent = parentEntitySet.addNew({ id: strategyId });\n            parentEntitySet.attachOrGet(parent);\n\n            var entity = {\n                name: self.addItem().name,\n                shortDescription: self.addItem().shortDescription,\n                description: self.addItem().description\n            }\n\n            entity[self.parentPropName] = parent;\n\n            var newEntity = entitySet.addNew(entity);\n            entitySet.attachOrGet(newEntity);\n            entitySet.add(newEntity);\n\n            db.saveChanges().done(function (i) {\n                closePopup(o, e);                               \n                _loadData(self);\n                self.addItem (entitySet.addNew());\n                kopost.publish(\"ca.sp.entitiesChanged\", \"Add new Entity\");\n\n            }).fail(function (error) {\n                self.addItem (entitySet.addNew());\n                console.log('Error = ' + error);\n\n            });\n\n            \/\/detach the entities after save to avoid having them saved again\n            entitySet.detach(newEntity);\n            parentEntitySet.detach(parent);\n\n            e.stopPropagation();\n        };\n\n\n        return {\n            init: init,\n            setEditMode: setEditMode,\n            bindPopup: bindPopup,\n            cancelDelete: cancelDelete,\n            startEdit: startEdit,\n            cancel: cancel,\n            deleteEntity: deleteEntity,\n            saveEntity: saveEntity\n        }\n\n    }()));\n    return EntitiesViewModel;\n});\n<\/code><\/code><\/pre>\n<h2 class=\"caspFancy\" data-bind=\"text: $root.title\"><\/h2>\n<pre>\n        \n\n        \n            \n            <\/pre>\n<ul data-bind=\"foreach: entities\">\n<li data-bind=\"css: {caspEditable: $root.editMode(),caspListSeperator: !$root.editMode()}\">\n<p data-bind=\"text: shortDescription\">\n<\/li>\n<\/ul>\n<pre>\n            \n\n            \n            <\/pre>\n<ul data-bind=\"foreach: parentChildArr\">\n<li data-bind=\"css: {caspEditable: $root.editMode()}\"><code>:<\/code>\n<ul data-bind=\"foreach:children\"><\/ul>\n<\/li>\n<\/ul>\n<pre>\n                            \n\n        \n        \n\n\/\/HERE IS THE PROBLEM - '$root' becomes the windows object when i open this popup\n \n             \n\n                Done\n                Cancel\n            \n            \n            \n             <\/pre>\n<h1 class=\"caspFancy\"><code>Add New<\/code><\/h1>\n<pre>\n\n         \n         \n            \n                Title\n                \n            \n            \n                Overview\n                \n            \n            \n                Mission\n                \n            \n            \n                Period\n                \n            \n            \n                Owned By\n                \n            \n            \n                Assigned to\n                 \n            \n        \n        \n    \n    \n\n\n\n\n\n\n\n<\/pre>\n<pre data-bind=\"text:ko.toJSON($root,null,2)\"><\/pre>\n<pre>\n<\/pre>\n<p id=\"rop\"><small>Originally posted 2013-12-05 09:41:55. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to debug ko (version: 2.2.1) using tag: but I&#8217;m getting this: Uncaught Error: Unable to parse bindings. Message: TypeError: Object object has no method &#8216;getType&#8217;; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1937","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1937\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}