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

File upload support #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

File upload support #4

wants to merge 1 commit into from

Conversation

cilphex
Copy link

@cilphex cilphex commented May 31, 2016

In conjunction with the bamboo-sync-ajax PR, this change allows one-way file uploads on a model by recognizing a special form_data field. This field is intended to be an HTML5 FormData object. If present, it is used as the request payload instead of the model's other fields. This allows multipart data to be sent to the server for things like file uploads. Or, a save of a model's attributes directly from a form instead of setting its properties explicitly:

Dommit example:

<form on-submit="on_submit">
    <!-- fields here -->
</form>
var Model = require('bamboo/model');
var ajax = require('bamboo-sync-ajax');

var SomeBambooModel = Model({
    status: String,
    reason: String,
    form_data: FormData // Only used one-way, for saving
}, {
    url_root: '/some-bamboo-model',
    sync: ajax,
    form_field: 'form_data'
});

module.exports = SomeBambooModel;
MyView.prototype.on_submit = function(ev, reactive) {
    var form = ev.target;
    var form_data = new FormData(form);
    var my_model = SomeBambooModel();
    my_model.form_data = form_data;
    my_model.save(function(err) {
        // Fields from the form above have been sent
    });
};

@defunctzombie
Copy link
Owner

Maybe better to put this under a flag in the model options. So when you initialize the model you pass options (right now ajax, url_root). Could add a new one called form_field or something like that. This would then be the key for which field you want to use as a form passthrough. Makes it safer for existing users that might have such a field already.

Also, not sure how I feel about this in general. Seems that in the case of form data, you wouldn't be using a model layer per-se and would just use something else to post the form content.

@cilphex
Copy link
Author

cilphex commented Jun 1, 2016

I think it makes sense when you want the model to be updated with the response data.

I'll think about reworking it into a flag in the options.

…ey for a model field to be used as a form passthrough
@cilphex
Copy link
Author

cilphex commented Jun 1, 2016

@defunctzombie I like that idea, I made the change, check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants