   Config
 ) ->
   class CommandStore
-    commands: null
+    commands: []
+    lastSync: null
     fileEntry: null
+
     constructor: ->
       _.extend this, Backbone.Events
       @requestFileSystem(

      reader.readAsText file
      self = this
      reader.onloadend = ->
        self.commands = JSON.parse(@result) if @result
        self.trigger "initialised"

    storeCommands: ->
      @fileEntry.createWriter ((fileWriter) =>
        blob = new Blob [JSON.stringify @commands], type: "application/json"

        fileWriter.write blob
        fileWriter.onwriteend = console.log.bind console, "Successfully wrote file"
        fileWriter.onerror = console.log.bind console, "Error writing file"
      ), console.log.bind(console, "Error creating writer")


      @fs.root.getFile "commands.json", { create: true }, ((fileEntry) =>
        @fileEntry = fileEntry
        fileEntry.file @readFile.bind(this), @onLoadError.bind(this)
      ), console.log.bind(console, "Error loading file")

    sync: ->
      params = {}
      params["updatedSince"] = @lastSync if @lastSync

      $.getJSON("#{Config.API_URL}/commands", params)
        .success((response) =>
          if response.length
            @commands = response.concat @commands
            @lastSync = _.last(response).updatedAt # Response is sorted by
                                                   # ascending updatedAt
            @storeCommands()

          @trigger "synced"
        )
        .error(console.log.bind(console, "Error fetching commands"))

    requestFileSystem: (
      window.requestFileSystem ||
