Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows offline #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions client/functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,21 @@ Cloudinary =
reader.readAsDataURL file

_upload_file: (file, ops={}, callback) ->
# Create collection document ID
collection_id = Random.id()

# Set fields
fields = _.extend ops.fields || {},
_id: collection_id
status: 'waiting'
preview: file

# Insert local preview
Cloudinary.collection.insert fields

Meteor.call "c.sign", ops, (error,result) ->
if error
Cloudinary.collection.remove collection_id
return callback and callback error,null

# Build form
Expand All @@ -78,20 +91,13 @@ Cloudinary =

form_data.append "file",file

# Create collection document ID
collection_id = Random.id()

# Send data
Cloudinary.xhr = new XMLHttpRequest()

Cloudinary.collection.insert
_id:collection_id
status:"uploading"
preview:file

Cloudinary.xhr.upload.addEventListener "progress", (event) ->
Cloudinary.collection.update _id:collection_id,
Cloudinary.collection.update collection_id,
$set:
status: 'uploading'
loaded:event.loaded
total:event.total
percent_uploaded: Math.floor ((event.loaded / event.total) * 100)
Expand All @@ -100,7 +106,7 @@ Cloudinary =
Cloudinary.xhr.addEventListener "load", ->
if Cloudinary.xhr.status < 400
response = JSON.parse @response
Cloudinary.collection.upsert collection_id,
Cloudinary.collection.update collection_id,
$set:
status:"complete"
percent_uploaded: 100
Expand All @@ -109,7 +115,7 @@ Cloudinary =
callback and callback null,response
else
response = JSON.parse @response
Cloudinary.collection.upsert collection_id,
Cloudinary.collection.update collection_id,
$set:
status:"error"
response: response
Expand All @@ -118,15 +124,15 @@ Cloudinary =

Cloudinary.xhr.addEventListener "error", ->
response = JSON.parse @response
Cloudinary.collection.upsert collection_id,
Cloudinary.collection.update collection_id,
$set:
status:"error"
response: response

callback and callback response,null

Cloudinary.xhr.addEventListener "abort", ->
Cloudinary.collection.upsert collection_id,
Cloudinary.collection.update collection_id,
$set:
status:"aborted"

Expand Down