Skip to content

Instantly share code, notes, and snippets.

@leepfrog
Created December 14, 2012 10:55
Show Gist options
  • Select an option

  • Save leepfrog/4284571 to your computer and use it in GitHub Desktop.

Select an option

Save leepfrog/4284571 to your computer and use it in GitHub Desktop.
addImages: (files) ->
formData = new FormData()
# Attach block id
formData.append "attachments[book_id]", @get('content.id')
# Loop through the FileList and render image files as thumbnails.
for file in files
# Only process image files.
if file.type.match("image.*")
formData.append "attachments[files][#{file.name}]", file
xhr = new XMLHttpRequest()
xhr.open "POST", "/attachments", true
# Upload progress indicator
# xhr.onprogress = (e) ->
# position = e.position || e.loaded
# total = e.totalSize || e.total
# percent = ((e.loaded/e.total)*100)+""
# console.log(percent)
# console.log(position, total)
# console.log(e)
# Server response
xhr.onload = (e) =>
attachments = @target.store.loadMany App.Attachment, JSON.parse(e.currentTarget.responseText).attachments
# !!!: This exists because when loading data, the associations on the flip-side are not auto-loaded
attachments.ids.forEach (id) =>
attachment = App.Attachment.find(id)
attachment.set 'book', @get('content')
attachment.get('stateManager').send('becameClean')
xhr.send formData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment