           @lastSync = response[response.length - 1].updatedAt
 
           @storeCommands()
-          event = if wasFirstSync then "load.commands" else "sync.commands"
-          @trigger event, @commands
+          eventName = if wasFirstSync then "load.commands" else "sync.commands"
+          @trigger eventName, @commands
       )
-      .error(console.log.bind(console, "Error fetching commands"))
+      .fail(console.log.bind(console, "Error fetching commands"))
 
   trigger: (eventName, eventData) ->
     window.Events.sendTrigger eventName, eventData

    params = {}
    params["updatedSince"] = @lastSync if @lastSync

    $.getJSON("http://dev.api.backtick.io/commands", params)
      .success((response) =>
        wasFirstSync = @commands.length is 0
        if response.length
          @mergeCommands response


class CommandStore
  @COMMANDS_ENDPOINT: "http://dev.api.backtick.io/commands"

  commands: []
  lastSync: null

  init: (ready) ->
    chrome.storage.local.get @storageLoaded

  storageLoaded: (storage) =>
    @commands = storage.commands or @commands
    @lastSync = storage.lastSync or @lastSync
    @trigger("load.commands", @commands)  if @commands.length
    @sync()

  storeCommands: ->
    chrome.storage.local.set {commands: @commands, lastSync: @lastSync}

  mergeCommands: (updatedCommands) ->
    commandsMap = {}
