This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
115 lines (98 loc) · 3.63 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$(function () {
var api = function() {
var sendFile = function (input, url) {
return AjaxFile.send({
method: "POST",
url: url,
desiredResponseDataType: AjaxFile.DataType.Json,
files: [
{ name: 'joeFile', element: input.getElement() }
],
data: {
name: 'joe',
data: {
lastname: 'bob',
age: 5
},
list: [
{ lastname: 'indien1', age: 1 },
{ lastname: 'indien2', age: 2 }
]
}
});
};
var sendFileWithJQuery = function (input, url) {
return $.Deferred(function (defer) {
$.fn.ajaxWithFile({
type: "POST",
url: url,
dataType: "json",
files: [
{ name: 'joeFile', element: input.getElement() }
],
success: function (json) {
defer.resolve({ data: json });
},
error: function (jqXhr, textStatus, errorThrown) {
var json = JSON.parse(jqXhr.responseText);
defer.reject({ data: json, error: errorThrown });
}
});
}).promise();
};
this.sendFileToWebApi = function (file) {
return sendFile(file, "/api/file");
};
this.sendFileToWebApiWithError = function (file) {
return sendFile(file, "/api/errorfile");
};
this.sendFileToWebApiWithJQuery = function (file) {
return sendFileWithJQuery(file, "/api/file");
};
this.sendFileToNancy = function(file) {
return sendFile(file, '/sendFile');
};
this.downloadFileToNancy = function (file) {
return sendFile(file, '/downloadFile');
};
this.downloadFileToNancyWithError = function (file) {
return sendFile(file, '/downloadFileWithError');
};
};
var demoMainViewModel = function (api) {
var rthis = this;
rthis.selectedFile = ko.observable();
rthis.result = ko.observable();
rthis.errorMessages = ko.observableArray();
var processRequest = function(methodeName) {
api[methodeName](rthis.selectedFile()).done(function (result) {
rthis.result(result.data);
rthis.errorMessages([]);
rthis.selectedFile(null);
}).fail(function (errors) {
rthis.result(null);
rthis.errorMessages(errors.data || 'error');
});
};
rthis.sendFileToWebApi = function () {
processRequest('sendFileToWebApi');
};
rthis.sendFileToWebApiWithError = function () {
processRequest('sendFileToWebApiWithError');
};
rthis.sendFileToWebApiWithJQuery = function () {
processRequest('sendFileToWebApiWithJQuery');
};
rthis.sendFileToNancy = function () {
processRequest('sendFileToNancy');
};
rthis.downloadFileToNancy = function () {
processRequest('downloadFileToNancy');
};
rthis.downloadFileToNancyWithError = function () {
processRequest('downloadFileToNancyWithError');
};
};
var mainViewModel = new demoMainViewModel(new api());
ko.applyBindings(mainViewModel);
});