{"id":1938,"date":"2022-08-30T15:20:42","date_gmt":"2022-08-30T15:20:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/knockoutjs-and-binding-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:42","modified_gmt":"2022-08-30T15:20:42","slug":"knockoutjs-and-binding-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/knockoutjs-and-binding-collection-of-common-programming-errors\/","title":{"rendered":"Knockoutjs and binding-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m going crazy with knockuoutjs and binding:<\/p>\n<p>I have defined a CreateEditGroup.js document and I have created methods and Collection to retrieve or update a Group in my application:<\/p>\n<pre><code>var url = window.location.pathname;\nvar GroupID = url.substring(url.lastIndexOf('\/') + 1);\n\n\nvar Group = function (group)\n{\n    var self = this;\n    self.GroupID = ko.observable(group ? group.GroupID : 0).extend({ required: true });\n    self.Name = ko.observable(group ? group.Name : '').extend({ required: true });\n};\n\nvar GroupCollection = function () {\n    var self = this;\n    if (GroupID == 0) {\n        self.group = ko.observable(new Group());\n    }\n    else {\n        $.ajax({\n            url: '\/Group\/GetGroupByID\/' + GroupID,\n            async: false,\n            dataType: 'json',\n            success: function (json) {\n                self.group = ko.observable(new Group(json));\n            }\n        });\n    }\n\n    self.backToGroupList = function () { window.location.href = '\/App\/Groups' };\n\n    \/\/Aggiunta o modifica\n\n    self.saveGroup = function () {\n        $.ajax({\n            type: (self.group().GroupID &gt; 0 ? 'PUT' : 'POST'),\n            cache: false,\n            dataType: 'json',\n            url: urlContact + (self.group().GroupID &gt; 0 ? '\/UpdateGroup?id=' + self.group().GroupID : '\/SaveGroup'),\n            data: JSON.stringify(ko.toJS(self.group())),\n            contentType: 'application\/json; charset=utf-8',\n            async: false,\n            success: function (data) {\n                window.location.href = '\/App\/Groups';\n            },\n            error: function (err) {\n                var err = JSON.parse(err.responseText);\n                var errors = \"\";\n                for (var key in err) {\n                    if (err.hasOwnProperty(key)) {\n                        errors += key.replace(\"group.\", \"\") + \" : \" + err[key];\n                    }\n                }\n                $(\"\").html(errors).dialog({ modal: true, title: JSON.parse(err.responseText).Message, buttons: { \"Ok\": function () { $(this).dialog(\"close\"); } } }).show();\n            },\n            complete: function () {\n            }\n        });\n    };\n\n};\n\nko.applyBindings(new GroupCollection());\n<\/code><\/pre>\n<p>And the view that shows the form have this HTML code:<\/p>\n<pre><code>@{\n    ViewBag.Title = \"CreateEditGroup\";\n}\n\n<\/code><\/pre>\n<h2><code>Nuovo gruppo<\/code><\/h2>\n<pre>\n<\/pre>\n<table class=\"table\">\n<tr>\n<th colspan=\"1\"><\/th>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<\/table>\n<p><code>Salva<\/code><\/p>\n<p>Everytime that I load the CreateEditGroup page I receive the message that is impossibile to bind Name attribute, but the code seems good.<\/p>\n<p>Help me, please.<\/p>\n<p>Code error:<\/p>\n<pre><code>An unhandled exception occurred at line 1936 column 17 in http:\/\/localhost:2297\/Scripts\/knockout-2.2.1.debug.js\n\n0x800a139e - Run-time JavaScript: Unable to parse bindings.\n\nMessage: ReferenceError: 'Name' is not defined;\n\nBindings value: value: Name\n<\/code><\/pre>\n<ol>\n<li>\n<p>I have solved this puzzle! The error is simply: in CreateEditGroup.js I have declared a variable that have called Group and an object called group<\/p>\n<pre><code>var Group = function (group)\n{\n    var self = this;\n    self.GroupID = ko.observable(gruppo ? gruppo.GroupID : 0).extend({ required: true });\n    self.Name = ko.observable(gruppo ? gruppo.Name : '').extend({ required: true });\n};\n<\/code><\/pre>\n<p>I have modified the name of the object passed in this function with another name and finally works!<\/p>\n<pre><code>var Group = function (gruppo)\n{\n    var self = this;\n    self.GroupID = ko.observable(gruppo ? gruppo.GroupID : 0).extend({ required: true });\n    self.Name = ko.observable(gruppo ? gruppo.Name : '').extend({ required: true });\n};\n<\/code><\/pre>\n<p>Thank you all for the help!<\/p>\n<\/li>\n<li>\n<p>I believe you have a capitalization error.<\/p>\n<pre><code>data-bind=\"with : Group\"\n<\/code><\/pre>\n<p>Should be<\/p>\n<pre><code>data-bind=\"with : group\"\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-05 09:42:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m going crazy with knockuoutjs and binding: I have defined a CreateEditGroup.js document and I have created methods and Collection to retrieve or update a Group in my application: var url = window.location.pathname; var GroupID = url.substring(url.lastIndexOf(&#8216;\/&#8217;) + 1); var Group = function (group) { var self = this; self.GroupID = ko.observable(group ? group.GroupID : [&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-1938","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1938","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=1938"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1938\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}