{"id":1083,"date":"2022-08-30T15:12:06","date_gmt":"2022-08-30T15:12:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/define-a-public-method-in-a-backbone-model-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:06","modified_gmt":"2022-08-30T15:12:06","slug":"define-a-public-method-in-a-backbone-model-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/define-a-public-method-in-a-backbone-model-collection-of-common-programming-errors\/","title":{"rendered":"Define a public method in a backbone model-Collection of common programming errors"},"content":{"rendered":"<p>I need to iterate over a backbone collection and get an array of objects that are derived from the models within the collection.<\/p>\n<p>The problem is that I do not know how to let the collection access a method defined when creating the model definition inside the object given in: <code>Backbone.Model.extend({})<\/code><\/p>\n<p><strong>This is an example of the architecture that I have so far:<\/strong><\/p>\n<pre><code>\/\/ THE MODEL\nvar TheModel = Backbone.Model.extend({\n    \/\/ THE FOLLOWING IS NOT AVAILABLE AS A METHOD OF THE MODEL:\n    \/\/     This isn't the actual method, but I though it might be helpful to\n    \/\/     to demonstrate that kind of thing the method needs to do (manipulate:\n    \/\/     map data and make it available to an external library)\n    get_derived_object: function(){\n        return this.get('lat') + '_' this.get('lon');\n    }\n\n});\n\n\/\/ THE COLLECTION:\nvar TheCollection = Backbone.Collection.extend({\n    \/\/ Use the underscore library to iterate through the list of\n    \/\/     models in the collection and get a list the objects returned from\n    \/\/     the \"get_derived_object()\" method of each model:\n    get_list_of_derived_model_data: function(){\n        return _.map(\n            this.models,\n            function(theModel){\n                \/\/ This method is undefined here, but this is where I would expect\n                \/\/     it to be:\n                return theModel.get_derived_object();\n\n            }\n        );\n    }\n\n});\n<\/code><\/pre>\n<p>I&#8217;m not sure where I am going wrong here, but I have a few guesses: * The collection is not iterating over it&#8217;s models in the way that it should * Public methods cannot be defined in Backbone.Model.extend({}) * I am looking in the wrong place for the public methods * Some other architectural mistake derived from a misunderstanding of how Backbone.js is intended to be used<\/p>\n<p>Any help is appreciated, thanks so much!<\/p>\n<p><strong>Edit<\/strong><\/p>\n<p>The problem was that there was actually a bug in this code. When the collection was populated, it did not have a reference to &#8220;TheModel&#8221; as its model type, so it created its own models.<\/p>\n<p>This line of code needed to be added when the collection was defined: <code>model: TheModel<\/code><\/p>\n<pre><code>var theCollection = Backbone.Collection.extend({\n    model: TheModel,\n    ...\n});\n<\/code><\/pre>\n<ol>\n<li>\n<p>Instead of using:<\/p>\n<pre><code>return _.map(\n    this.models,\n    function(theModel){\n        \/\/ This method is undefined here, but this is where I would expect\n        \/\/     it to be:\n        return theModel.get_derived_object();\n     }\n);\n<\/code><\/pre>\n<p>Why not use the built-in collection version:<\/p>\n<pre><code>return this.map(function(theModel){\n    return theModel.get_derived_object();\n});\n<\/code><\/pre>\n<p>Not sure if that will help, but it&#8217;s worth a shot.<\/p>\n<p>And for the record, all methods defined in the first argument to <code>new Backbone.Model(<\/code> are &#8220;public&#8221;, so you&#8217;ve got the basics right.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:22:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I need to iterate over a backbone collection and get an array of objects that are derived from the models within the collection. The problem is that I do not know how to let the collection access a method defined when creating the model definition inside the object given in: Backbone.Model.extend({}) This is an example [&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-1083","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1083","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=1083"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1083\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}