     )
 
   fetchFile: (fileId) ->
-    that = this
-    return new Promise((resolve, reject) ->
-      that.gdrive.get(fileId)
-        .then (file) ->
-          that.set("file", file)
-          that.set('title', file.title)
-
-          that.gdrive.getFileResource(
+    return new Promise((resolve, reject) =>
+      this.gdrive.get(fileId)
+        .then (file) =>
+          this.set("file", file)
+          this.set('title', file.title)
+
+          this.gdrive.getFileResource(
             file.downloadUrl,
-            that.get('access_token')
+            this.get('access_token')
           )
-        .then (fileContent) ->
-          that.set('content', fileContent)
+        .then (fileContent) =>
+          this.set('content', fileContent)
           resolve(fileContent)
       )
 

      return Promise.resolve('insert')

  insert: (title, content) ->
    that = this
    gapi.client.load('drive', 'v2')
      .then () -> that.gdrive.insert(title, content)
      .then (response) ->
        console.log("insert file")
        console.log(response.result)
        that.set("file", response.result)

        Promise.resolve(response.result)

  update: (title, content) ->
    that = this
    fileId = that.get('file').id
    gapi.client.load('drive', 'v2')
      .then () -> that.gdrive.update(fileId, title, content)
      .then (response) ->
        console.log("update file")
        console.log(response.result)
        that.set("file", response.result)

        Promise.resolve(response.result)
})

    this.set("client_id", this.gdrive.CLIENT_ID)

  authorize: ()->
    return new Promise((resolve, reject) =>
      reject(null) unless gapi.auth

      gapi.auth.authorize({
        'client_id': this.gdrive.CLIENT_ID,
        'scope': this.gdrive.SCOPES,
        'immediate': true },
        (authResult) =>
          # immediate: trueが失敗したときはflaseで再チャレンジ
          # 今度はダイアログが開く
          if (authResult && !authResult.error)
            console.log("immediate true")
            this.set("access_token", authResult.access_token)
            resolve(authResult)
          else
            console.log("immediate false")
            gapi.auth.authorize({
              'client_id': this.gdrive.CLIENT_ID,
              'scope': this.gdrive.SCOPES,
              'immediate': false },
              (authResult) =>
                if (authResult && !authResult.error)
                  this.set("access_token", authResult.access_token)
                  resolve(authResult)
                else
                  console.log("authorize failed")
