-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSimpleCRUD.mxml
171 lines (140 loc) · 5.43 KB
/
SimpleCRUD.mxml
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import spark.events.GridSelectionEvent;
//It is strongly recommended to set the rules for this node to be read and write by everyone so you can quickly test it
private static const JOURNAL_URL:String = "https://<YOUR-PROJECT-ID>.firebaseio.com/journal";
protected function init(event:FlexEvent):void
{
loadJournal();
}
protected function selectItem(event:GridSelectionEvent):void
{
titleInput.text = journalGrid.selectedItem.title;
descriptionInput.text = journalGrid.selectedItem.description;
deleteBtn.enabled = true;
modifyBtn.enabled = true;
}
/*
Read block
*/
private function loadJournal():void
{
var request:URLRequest = new URLRequest(JOURNAL_URL+".json");
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, journalLoaded);
loader.load(request);
}
private function journalLoaded(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, journalLoaded);
//The JSON generated by Firebase contains the id as the node key, we use this function to add it to our Objects
var rawData:Object = JSON.parse(event.currentTarget.data);
var entriesArray:Array = new Array();
for (var parent:String in rawData)
{
var tempObject:Object = new Object();
tempObject.id = parent;
for (var child:* in rawData[parent])
{
tempObject[child] = rawData[parent][child];
}
entriesArray.push(tempObject);
tempObject = null;
}
journalGrid.dataProvider = new ArrayList(entriesArray);
titleInput.text = "";
descriptionInput.text = "";
deleteBtn.enabled = false;
modifyBtn.enabled = false;
}
/*
Insert block
*/
private function saveEntry():void
{
var myObject:Object = new Object();
myObject.title = titleInput.text;
myObject.description = descriptionInput.text;
myObject.timestamp = new Date().getTime();
var request:URLRequest = new URLRequest(JOURNAL_URL+".json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entrySent);
loader.load(request);
}
private function entrySent(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entrySent);
trace(event.currentTarget.data);
loadJournal();
}
/*
Delete block
*/
private function deleteEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "DELETE");
var request:URLRequest = new URLRequest(JOURNAL_URL+"/"+journalGrid.selectedItem.id+".json");
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryDeleted);
loader.load(request);
}
private function entryDeleted(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entryDeleted);
trace(event.currentTarget.data);
loadJournal();
}
/*
Update block
*/
private function updateEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "PATCH");
var myObject:Object = new Object();
myObject.title = titleInput.text;
myObject.description = descriptionInput.text;
var request:URLRequest = new URLRequest(JOURNAL_URL+"/"+journalGrid.selectedItem.id+".json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryUpdated);
loader.load(request);
}
private function entryUpdated(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entryUpdated);
trace(event.currentTarget.data);
loadJournal();
}
]]>
</fx:Script>
<s:VGroup left="10" right="10" top="10" bottom="10">
<s:DataGrid id="journalGrid" selectionChange="selectItem(event)" width="100%" height="100%">
<s:columns>
<s:ArrayList>
<s:GridColumn headerText="ID" dataField="id" />
<s:GridColumn headerText="Title" dataField="title" />
<s:GridColumn headerText="Description" dataField="description" />
<s:GridColumn headerText="Timestamp" dataField="timestamp" />
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:TextInput id="titleInput" prompt="Type a Title" width="100%"/>
<s:TextArea id="descriptionInput" prompt="Type a Description" width="100%" height="100"/>
<s:HGroup width="100%" horizontalAlign="center">
<s:Button id="deleteBtn" label="Delete" click="deleteEntry()"/>
<s:Button id="modifyBtn" label="Modify" click="updateEntry()"/>
<s:Button id="createBtn" label="Create" click="saveEntry()"/>
</s:HGroup>
</s:VGroup>
</s:WindowedApplication>