bamboo
bamboo copied to clipboard
File upload support
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
});
};
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.
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.
@defunctzombie I like that idea, I made the change, check it out.