{"id":233,"date":"2012-07-24T09:39:04","date_gmt":"2012-07-24T16:39:04","guid":{"rendered":"http:\/\/adam.yanalunas.com\/blog\/?p=233"},"modified":"2012-07-24T09:39:04","modified_gmt":"2012-07-24T16:39:04","slug":"the-dude-abides","status":"publish","type":"post","link":"http:\/\/adam.yanalunas.com\/blog\/archives\/233","title":{"rendered":"The dude abides"},"content":{"rendered":"<p>Man, when did forms get so complex? Oh, right, always.<\/p>\n<p>Backbone.js is great because it lets me structure my app how I see best and gives me the tools to do so efficiently. Well, most of the tools. It&#8217;s not very good at dealing with handling multiple forms submitting over AJAX and orchestrating those submissions under a parent view. Nor should it be. However, I needed that functionality so I wrote some code.<\/p>\n<h3>I give you Abide<\/h3>\n<p>Abide is an extension of a Backbone.View that has hooks for checking model validation, dealing with multiple jQuery.Deferred promises (be they AJAX or not) and orchestrating the whole submission process in one place. <\/p>\n<p>Just have your view extend Abide instead of Backbone.View&hellip;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar parentView,\r\n    firstChild,\r\n    secondChild,\r\n    redhadedStepChild;\r\n\r\n\/\/Create up your parent view and extend Abide instead of Backbone.View\r\nparentView = Backbone.Abide.extend({\r\n    promises: function() {\r\n        \/\/ Gather child view save promises\r\n    }\r\n}\r\n});\r\n\r\n\/\/Create child views as well. These can be regular views or more Abide parents.\r\nfirstChild = Backbone.View.extend({ \/* ... *\/ });\r\nsecondChild = Backbone.View.extend({ \/* ... *\/ });\r\nredhadedStepChild = Backbone.View.extend({ \/* ... *\/ });\r\n\r\n\/\/Then instantiate the parent, passing in which views it should parent\r\nnew parentView({ views: [firstChild, secondChild, redhadedStepChild] });\r\n<\/pre>\n<p>&hellip;and override the promises method to return your view&#8217;s promises&hellip;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar sweetAbideView = Backbone.Abide.extend({\r\n    promises: function() {\r\n        var promises = [],\r\n            self = this;\r\n\r\n        _.each(self.views, function(view) {\r\n            promises.push(view.model.save());\r\n        });\r\n\r\n        return promises;\r\n    }\r\n});\r\n<\/pre>\n<p>&hellip;and you&#8217;re good to go!<\/p>\n<p>There&#8217;s more information on <a href=\"https:\/\/github.com\/adamyanalunas\/backbone.abide.js\">github<\/a>. Fork and submit pull requests!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Man, when did forms get so complex? Oh, right, always. Backbone.js is great because it lets me structure my app how I see best and gives me the tools to do so efficiently. Well, most of the tools. It&#8217;s not very good at dealing with handling multiple forms submitting over AJAX and orchestrating those submissions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/posts\/233"}],"collection":[{"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/comments?post=233"}],"version-history":[{"count":3,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":236,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions\/236"}],"wp:attachment":[{"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/adam.yanalunas.com\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}