forked from cmather/meteor-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeteor-file-uploader.js
75 lines (60 loc) · 1.87 KB
/
meteor-file-uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/************************ FileUploader ***************************************/
FileUploader = function (options) {
this.options = options || {};
this.method = options.method;
this.template = options.template;
this.files = new Meteor.Collection(null);
};
FileUploader.prototype = {
constructor: FileUploader,
render: function (data) {
if (!this.template) return;
var self = this;
var html;
html = Spark.isolate(function () {
return self.template(_.extend({
files: function () {
return self.files.find();
}
}, data || {}));
});
html = Spark.attachEvents({
'change input[type=file]': function (e, tmpl) {
var fileInput = e.currentTarget;
var file;
var mFile;
for (var i = 0; i < fileInput.files.length; i++) {
file = fileInput.files[i];
mFile = new MeteorFile(file, {
collection: self.files
});
self.files.insert(mFile.toJSONValue());
mFile.upload(
file,
self.method,
self.options,
self.options.callback || function (err) {
if (err) throw err;
}
);
}
}
}, html);
return html;
}
};
/*****************************************************************************/
/************************ Handlebars *****************************************/
Handlebars.registerHelper('FileUploader', function (options) {
var uploadOptions = options.hash;
var uploader = new FileUploader(_.extend({
template: options.fn
}, uploadOptions, {
size: eval(uploadOptions.size)
}));
return uploader.render(options.data || {});
});
Handlebars.registerHelper('humanize', function (number, options) {
return MeteorFile.humanize(number);
});
/*****************************************************************************/