-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (97 loc) · 2.51 KB
/
app.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
116
App = {
web3Provider: null,
contracts: {},
account : "0x0",
init: async function()
{
return await App.initWeb3();
},
initWeb3: async function()
{
if(typeof web3 != 'undefined')
{
App.web3Provider = web3.currentProvider;
web3 = new Web3(web3.currentProvider);
}
else
{
App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');
web3 = new Web3(App.web3Provider);
}
return App.initContract();
},
initContract: function() {
$.getJSON("../Person.json", function(person){
App.contracts.Person = TruffleContract(person);
App.contracts.Person.setProvider(App.web3Provider);
}).done(function(){
App.getPersonFun();
return App.bindEvents();
});
},
bindEvents: function() {
web3.eth.getCoinbase(function(err, account){
if(err==null){
App.accounts = account;
}
});
},
addPersonFun: async function()
{
App.contracts.Person.deployed().then(async function(instance){
var namePerson = document.getElementById('personName').value;
if(namePerson=="")
{
alert("Input The Person Name");
}
else
{
await instance.addPerson(namePerson.toString(), {from: App.accounts});
App.getPersonFun();
}
}).catch(function(err)
{
location.reload();
});
},
deletePersonFun: async function()
{
App.contracts.Person.deployed().then(async function(instance){
var delID = document.getElementById('personID').value;
if(delID=="")
{
alert("Input The Person ID to delete");
}
else
{
await instance.deletePerson(parseInt(delID), {from: App.accounts});
App.getPersonFun();
}
});
}
,
getPersonFun: async function()
{
App.contracts.Person.deployed().then(async function(instance){
var count = await instance.getCount();
if(count==0)
{
document.getElementById('getData').innerHTML = "<tr><td>N/A</td><td>N/A</td><td>N/A</td></tr>";
}
else {
document.getElementById('getData').innerHTML = "";
for (var i = 0; i < count; i++) {
var person = await instance.getPerson(i);
if(person[0].toNumber()!=0) {
document.getElementById('getData').innerHTML += "<tr><td>" + person[0] + "</td><td>" + person[1] + "</td><td>" + person[2] + "</td></tr>";
}
}
}
});
},
};
$(function() {
$(window).load(function() {
App.init();
});
});