Search code examples
javascriptrestbackbone.jssingle-page-applicationmustache

Backbone JS Single Page Application file upload?


I'm looking for some design patterns or best practices around uploading files from a form to a REST end point.

The REST endpoint is already working and I can POST files to it. Any thoughts would really be appreciated.


Solution

  • Is that what you're looking for?

    var FileUploadView = Backbone.View.extend({
        events: {
            "click #submit": function() {
                $.ajax({
                    url: '%YOUR_URL%',
                    type: 'POST',
                    data: new FormData(this.$('#form')[0]),
                });
            }
        },
        render: function() {
            this.$el.html("<form id='form'><input type='file' /><input id='submit' value='Upload File' type='button' /></form>");
        }
    };