Skip to content

Instantly share code, notes, and snippets.

Last active December 14, 2015 10:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Single Page Apps fight code snippets

I'm participating in a "Single Page Apps fight" on the wroclove.rb conference and we don't want to make too much noise with the slides, so here are a few snippets that may make it easier to understand my points.

// This is an example from Rdio, live backbone application. This function
// is supposed to clean up the view before removing it. As you can see
// you need to do a lot of housekeeping yourself and if you forget to
// remove any event listeners, you just introduced a memory leak.
// I'm not saying that backbone is totally useless, because of that fact,
// but I would definitely not use it in any large and dynamic application.
// It's suitable for small additions for mainly static websites.
destroy: function () {
var c = this;
try {
this._element.removeEventListener("error", this._triggerError),
this._element.removeEventListener("ended", this._triggerEnd),
this._element.removeEventListener("canplay", this._triggerReady),
this._element.removeEventListener("loadedmetadata", this._onLoadedMetadata),
_.each(b, function (a) {
c._element.removeEventListener(a, c._bubbleProfilingEvent)
_.each(a, function (a) {
c._element.removeEventListener(a, c._logEvent)
this._element = null,
} catch (d) {}
# This is an example from the hexagonal workshop on wroclove.rb:
# The problem here is similar to the one in backbone. This code is
# supposed to do a few really simple thing and even though it needs
# a lot of housekeeping and manual views and events management.
class Gui
constructor: ->
@container = $("body")
showNameForm: ->
html = JST['templates/name_form']
form = @container.find("#name-form")
form.submit (e) =>
name = form.find("input[name='name']").val()
removeNameForm: ->
nameFormSubmitted: (name) ->
showGreeting: (name) ->
html = JST['templates/greeting'](name: name)
removeGreeting: ->
showRestartButton: ->
button = $("<input type='button' value='restart'>")
$("body").append(button) (e) =>
restartClicked: ->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment