Skip to content

Commit 54b28fa

Browse files
committed
Hikr refactors
1 parent 33a7dfd commit 54b28fa

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

chapter-7/Components/hikr.Button.ux

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<string ux:Property="Text" />
33

44
<Rectangle Layer="Background" Color="#125F63" CornerRadius="4">
5-
<DropShadow Angle="90" Distance="1" Spread="0.2" Size="2" Color="#00000060" />
5+
<Shadow Angle="90" Distance="1" Size="2" Color="#00000060" />
66
</Rectangle>
77

88
<hikr.Text Value="{ReadProperty Text}" FontSize="16" TextAlignment="Center" />

chapter-8/Components/hikr.Button.ux

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<float ux:Property="FontSize" />
44

55
<Rectangle Layer="Background" Color="#125F63" CornerRadius="4">
6-
<DropShadow Angle="90" Distance="1" Spread="0.2" Size="2" Color="#00000060" />
6+
<Shadow Angle="90" Distance="1" Size="2" Color="#00000060" />
77
</Rectangle>
88

99
<hikr.Text Value="{ReadProperty Text}" FontSize="{ReadProperty FontSize}" TextAlignment="Center" />

chapter-8/Modules/Backend.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ function getHikes() {
5252
function updateHike(id, name, location, distance, rating, comments) {
5353
return new Promise(function(resolve, reject) {
5454
setTimeout(function() {
55+
console.log("Updating hike in backend...");
5556
for (var i = 0; i < hikes.length; i++) {
5657
var hike = hikes[i];
5758
if (hike.id == id) {
59+
console.log("Found hike, updating");
5860
hike.name = name;
5961
hike.location = location;
6062
hike.distance = distance;
6163
hike.rating = rating;
6264
hike.comments = comments;
65+
console.log("New hike:" + JSON.stringify(hike));
6366
break;
6467
}
6568
}

chapter-8/Modules/Context.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,49 @@ var Backend = require("./Backend");
33

44
var hikes = Observable();
55

6+
function CreateObservableHike(backendHike) {
7+
var obj = {};
8+
obj.id = Observable(backendHike.id);
9+
obj.name = Observable(backendHike.name);
10+
obj.location = Observable(backendHike.location);
11+
obj.distance = Observable(backendHike.distance);
12+
obj.rating = Observable(backendHike.rating);
13+
obj.comments = Observable(backendHike.comments);
14+
15+
obj.save = function() {
16+
Backend.updateHike(obj.id.value, obj.name.value, obj.location.value, obj.distance.value, obj.rating.value, obj.comments.value)
17+
.catch(function(error) {
18+
console.log("Couldn't update hike: " + obj.id.value);
19+
});
20+
}
21+
obj.reset = function() {
22+
obj.id.value = backendHike.id;
23+
obj.name.value = backendHike.name;
24+
obj.location.value = backendHike.location;
25+
obj.distance.value = backendHike.distance;
26+
obj.rating.value = backendHike.rating;
27+
obj.comments.value = backendHike.comments;
28+
}
29+
return obj;
30+
}
31+
632
Backend.getHikes()
733
.then(function(newHikes) {
34+
for(var i = 0; i < newHikes.length; i++) {
35+
newHikes[i] = CreateObservableHike(newHikes[i]);
36+
}
837
hikes.replaceAll(newHikes);
938
})
1039
.catch(function(error) {
1140
console.log("Couldn't get hikes: " + error);
1241
});
13-
42+
/*
1443
function updateHike(id, name, location, distance, rating, comments) {
1544
for (var i = 0; i < hikes.length; i++) {
1645
var hike = hikes.getAt(i);
46+
console.log("Finding hike id " + id + ", current: " + hike.id);
1747
if (hike.id == id) {
48+
console.log("updating");
1849
hike.name = name;
1950
hike.location = location;
2051
hike.distance = distance;
@@ -28,10 +59,11 @@ function updateHike(id, name, location, distance, rating, comments) {
2859
.catch(function(error) {
2960
console.log("Couldn't update hike: " + id);
3061
});
31-
}
62+
}*/
3263

3364
module.exports = {
34-
hikes: hikes,
65+
hikes: hikes
66+
3567

36-
updateHike: updateHike
68+
//updateHike: updateHike
3769
};

chapter-8/Pages/EditHikePage.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
var Context = require("Modules/Context");
22

3-
var hike = this.Parameter;
3+
var hike = this.Parameter.map(function(x) { console.log("Fetching id " + x + ", length: " + Context.hikes.length); return Context.hikes.getAt(x)}); //TODO bug is that this isnt returning an observable?
44

5+
/*
56
var name = hike.map(function(x) { return x.name; });
67
var location = hike.map(function(x) { return x.location; });
78
var distance = hike.map(function(x) { return x.distance; });
89
var rating = hike.map(function(x) { return x.rating; });
910
var comments = hike.map(function(x) { return x.comments; });
10-
11+
*/
1112
function cancel() {
1213
// Refresh hike value to reset dependent Observables' values
13-
hike.value = hike.value;
14+
hike.value.reset();
1415
router.goBack();
1516
}
1617

1718
function save() {
18-
Context.updateHike(hike.value.id, name.value, location.value, distance.value, rating.value, comments.value);
19+
console.log("Saving hike");
20+
console.log("New name: " + hike.value.name.value);
21+
hike.value.save();
1922
router.goBack();
2023
}
2124

2225
module.exports = {
23-
name: name,
26+
/*name: name,
2427
location: location,
2528
distance: distance,
2629
rating: rating,
27-
comments: comments,
30+
comments: comments,*/
31+
hike: hike,
2832

2933
cancel: cancel,
3034
save: save

chapter-8/Pages/EditHikePage.ux

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515

1616
<StackPanel>
1717
<TitleText>Name:</TitleText>
18-
<hikr.TextBox Value="{name}" />
18+
<hikr.TextBox Value="{hike.name}" />
1919
</StackPanel>
2020

2121
<StackPanel>
2222
<TitleText>Location:</TitleText>
23-
<hikr.TextBox Value="{location}" />
23+
<hikr.TextBox Value="{hike.location}" />
2424
</StackPanel>
2525

2626
<StackPanel>
2727
<TitleText>Distance (km):</TitleText>
28-
<hikr.TextBox Value="{distance}" InputHint="Decimal" />
28+
<hikr.TextBox Value="{hike.distance}" InputHint="Decimal" />
2929
</StackPanel>
3030

3131
<StackPanel>
3232
<TitleText>Rating:</TitleText>
33-
<hikr.TextBox Value="{rating}" InputHint="Integer" />
33+
<hikr.TextBox Value="{hike.rating}" InputHint="Integer" />
3434
</StackPanel>
3535

3636
<StackPanel>
3737
<TitleText>Comments:</TitleText>
38-
<hikr.TextView Value="{comments}" TextWrapping="Wrap" />
38+
<hikr.TextView Value="{hike.comments}" TextWrapping="Wrap" />
3939
</StackPanel>
4040
</StackPanel>
4141
</ScrollView>

chapter-8/Pages/HomePage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Context = require("Modules/Context");
22

33
function goToHike(arg) {
44
var hike = arg.data;
5-
router.push("editHike", hike);
5+
router.push("editHike", hike.id.value);
66
}
77

88
module.exports = {

0 commit comments

Comments
 (0)