-
-
Save rainforest-of-code/2844510 to your computer and use it in GitHub Desktop.
Help me clean this up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the render method in the `ItemView` in my Backbone.Marionette plugin. | |
// It's a giant method - far too much happening in this one method, and poor | |
// use of memory with all of the methods that get re-defined on every use. | |
// | |
// It desperately needs to be cleaned up. But I can't do something quite as | |
// simple as just move the inner functions on to the ItemView itself. There | |
// are too many methods on the ItemView already. | |
// | |
// Suggestions? What are some good patterns to clean this up? | |
// | |
// Fork this gist or otherwise give me some ideas. | |
// | |
// Thanks, | |
// -Derick | |
render: function(){ | |
var that = this; | |
var deferredRender = $.Deferred(); | |
var beforeRenderDone = function() { | |
that.trigger("before:render", that); | |
that.trigger("item:before:render", that); | |
var deferredData = that.serializeData(); | |
$.when(deferredData).then(dataSerialized); | |
} | |
var dataSerialized = function(data){ | |
var asyncRender = that.renderHtml(data); | |
$.when(asyncRender).then(templateRendered); | |
} | |
var templateRendered = function(html){ | |
that.$el.html(html); | |
callDeferredMethod(that.onRender, onRenderDone, that); | |
} | |
var onRenderDone = function(){ | |
that.trigger("render", that); | |
that.trigger("item:rendered", that); | |
deferredRender.resolve(); | |
} | |
callDeferredMethod(this.beforeRender, beforeRenderDone, this); | |
return deferredRender.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment