Sha256: 26a352dbe384d2336298707ac872373835b5d010a3e8d8fa58d017601e95d4f0

Contents?: true

Size: 1.25 KB

Versions: 8

Compression:

Stored size: 1.25 KB

Contents

var FileItem = Backbone.Model.extend({
    name: 'photo',
    url: 'photos',
    idAttribute: '_id',
    defaults: {
        progress:0
    },
    initialize: function(){
        this.on('add', this.onAdd, this);
    },
    onAdd: function(file) {
        var _self = this;
                
        var xhr = new XMLHttpRequest();
        xhr.open("POST", this.url);
        var datas = new FormData();
        datas.append('image', this.get('image'));

        xhr.upload.addEventListener("progress", function(e) {
            if(e.lengthComputable) {
                var currentState = (e.loaded / e.total) * 100;
                _self.set({progress: currentState});
            }
        });
        
        xhr.onreadystatechange = function(e) {
            if(e.currentTarget.readyState == 4) {
                _self.set({progress: 100});
                _self.trigger('uploadCompleted');
                if(xhr.responseText){
                    var responseObject = JSON.parse(xhr.responseText);
                    _self.set(responseObject);
                } else {
                    console.log("Não foi possível enviar o arquivo");
                }
                
            }
        };

        // Start send the file.
        xhr.send(datas);
    }
});

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
mix-rails-0.22.0 mix-rails-albums/app/assets/javascripts/models/fileitem.js
mix-rails-albums-0.22.0 app/assets/javascripts/models/fileitem.js
mix-rails-0.16.0 mix-rails-albums/app/assets/javascripts/models/fileitem.js
mix-rails-albums-0.16.0 app/assets/javascripts/models/fileitem.js
mix-rails-0.15.0 mix-rails-albums/app/assets/javascripts/models/fileitem.js
mix-rails-albums-0.15.0 app/assets/javascripts/models/fileitem.js
mix-rails-0.12.2 mix-rails-albums/app/assets/javascripts/models/fileitem.js
mix-rails-albums-0.12.2 app/assets/javascripts/models/fileitem.js