-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathApp.controller.js
More file actions
83 lines (78 loc) · 3.58 KB
/
App.controller.js
File metadata and controls
83 lines (78 loc) · 3.58 KB
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
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator"
], function (Controller, JSONModel, Filter, FilterOperator) {
"use strict";
return Controller.extend("sap.codejam.controller.App", {
onInit: function () {
this.getView().setModel(new JSONModel({
itemSelected: false,
selectedQuantity: 1
}), "userSelection")
},
onSelect: function (oEvent) {
let oModel = this.getView().getModel("userSelection")
let selectedModelPath = oEvent.getSource().getBindingContext().sPath
let selectedModelData = oEvent.getSource().getModel().getProperty(selectedModelPath)
oModel.setProperty("/selectedItemPath", selectedModelPath)
oModel.setProperty("/selectedItemData", selectedModelData)
oModel.setProperty("/selectedQuantity", 1)
this.getView().byId("orderStatus").setText("")
oModel.setProperty("/itemSelected", true)
},
onSubmitOrder: function () {
let oView = this.getView()
let userSelectionData = oView.getModel("userSelection").getData()
let reqSettings = {
"url": "/browse/submitOrder",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"book": userSelectionData.selectedItemData.ID,
"quantity": userSelectionData.selectedQuantity
}),
}
let i18nModel = oView.getModel("i18n")
jQuery.ajax(reqSettings)
.done(function (response) {
console.log(response)
oView.byId("orderStatus")
.setText(
`${i18nModel.getProperty("orderSuccessful")}
(${userSelectionData.selectedItemData.title},
${userSelectionData.selectedQuantity}
${i18nModel.getProperty("pieces")})`
)
oView.byId("orderStatus").setState("Success")
let userSelectedPath = oView.getModel("userSelection").getProperty("/selectedItemPath")
oView.getModel().setProperty(userSelectedPath + "/stock", response.stock)
oView.getModel("userSelection").setProperty("/selectedItemData/stock", response.stock)
})
.fail(function(response) {
console.log(response)
oView.byId("orderStatus").setText(`${i18nModel.getProperty("Error")}`)
oView.byId("orderStatus").setState("Error")
})
},
onSearch: function (oEvent) {
var aFilter = [];
var sQuery = oEvent.getParameter("newValue");
if (sQuery) {
aFilter.push(new Filter("title", FilterOperator.Contains, sQuery));
}
var oList = this.byId("booksTable");
var oBinding = oList.getBinding("items");
oBinding.filter(aFilter);
let oModel = this.getView().getModel("userSelection")
oModel.setProperty("/selectedItemPath", {})
oModel.setProperty("/selectedItemData", {})
this.getView().byId("orderStatus").setText("")
oModel.setProperty("/itemSelected", false)
}
});
});