Skip to content

Commit a7d3310

Browse files
committed
added in some real ajax
1 parent e5d041e commit a7d3310

File tree

3 files changed

+85
-53
lines changed

3 files changed

+85
-53
lines changed

app/components/home/home.controller.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,49 @@
88

99
function homeController($scope, $http, $window, $q, asyncService) {
1010

11-
var vm = this;
12-
13-
//services
14-
vm.angularstrapService = asyncService;
15-
16-
asyncService.getHeroText();
17-
18-
// from async service
19-
vm.HeroHeader = asyncService.retrievedData.HeroHeader;
20-
vm.HeroText = asyncService.retrievedData.HeroText;
21-
22-
// subsections
23-
vm.col0heading = "Subsections";
24-
vm.col0text = "I may populate this with a microservice! Or have this be a separate view. This template uses Angular UI which is better than using the Angular router in my opinion.";
25-
26-
return vm;
27-
}
11+
var vm = this;
12+
13+
//services
14+
vm.angularstrapService = asyncService;
15+
16+
// get our hero text (hardcoded)
17+
asyncService.getHeroText()
18+
.then(function (data) {
19+
vm.HeroHeader = asyncService.heroData.HeroHeader;
20+
vm.HeroText = asyncService.heroData.HeroText;
21+
}, function (error) {
22+
vm.HeroHeader = "Error";
23+
vm.HeroText = error;
24+
});
25+
26+
// real ajax calls
27+
asyncService.getColumnData(1)
28+
.then(function (data) {
29+
vm.col1heading = data.title;
30+
vm.col1text = data.body;
31+
}, function (error) {
32+
vm.col1heading = "Error";
33+
vm.col1text = error.statusText;
34+
});
35+
36+
asyncService.getColumnData(2)
37+
.then(function (data) {
38+
vm.col2heading = data.title;
39+
vm.col2text = data.body;
40+
}, function (error) {
41+
vm.col2heading = "Error";
42+
vm.col2text = error.statusText;
43+
});
44+
45+
asyncService.getColumnData(3)
46+
.then(function (data) {
47+
vm.col3heading = data.title;
48+
vm.col3text = data.body;
49+
}, function (error) {
50+
vm.col3heading = "Error";
51+
vm.col3text = error.statusText;
52+
});
53+
54+
return vm;
55+
}
2856
})();

app/components/home/services/home.services.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,46 @@
66

77
asyncService.$inject = ['$http', '$q'];
88

9-
function asyncService($http, $q) {
10-
11-
var factory = {
12-
//properties
13-
retrievedData: [],
14-
getHeroText : getHeroText
9+
function asyncService($http, $q) {
10+
11+
var factory = {
12+
//properties
13+
heroData: [],
14+
getHeroText: getHeroText,
15+
getColumnData: getColumnData
16+
};
17+
18+
function getHeroText() {
19+
20+
var defer = $q.defer()
21+
22+
factory.heroData = {
23+
HeroHeader: "Hello AngularStrap!",
24+
HeroText: "This is the AngularStrap home page. This text is being pulled from a service, and can be populated by hand coding the property in the controller, dynamically or via services."
1525
};
26+
// this will always resolve because this is hardcoded
27+
defer.resolve();
1628

17-
function getHeroText() {
29+
return defer.promise
30+
}
1831

19-
// this is where we'd put some ajax calls
32+
function getColumnData(columnum) {
2033

21-
factory.retrievedData = {
22-
HeroHeader: "Hello AngularStrap!",
23-
HeroText: "This is the AngularStrap home page. This text is being pulled from a service, and can be populated by hand coding the property in the controller, dynamically or via services."
24-
};
34+
var defer = $q.defer()
2535

26-
//factory.retrievedData.HeroHeader = "Hello World!";
27-
//factory.retrievedData.HeroText = "This is the AngularStrap home page. This text is being pulled from a service, and can be populated by hand coding the property in the controller, dynamically or via services.";
36+
$http({
37+
method: 'GET',
38+
url: "http://jsonplaceholder.typicode.com" + "/posts/" + columnum
2839

40+
}).then(function successCallback(response) {
41+
defer.resolve(response.data);
42+
}, function errorCallback(response) {
43+
defer.reject(response);
44+
});
2945

30-
}
31-
return factory;
46+
return defer.promise
3247
}
48+
49+
return factory;
50+
}
3351
})();

app/components/home/views/subsection.view.html

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
<div class="row clearfix">
22
<div class="col-md-4 column">
33
<h2>
4-
{{ctrl.col0heading}}
4+
{{ctrl.col1heading}}
55
</h2>
6-
7-
<p> {{ctrl.col0text}}</p>
8-
6+
<p> {{ctrl.col1text}}</p>
97
<p>
108
<a class="btn" href="#">View details</a>
119
</p>
1210
</div>
1311
<div class="col-md-4 column">
1412
<h2>
15-
Heading
13+
{{ctrl.col2heading}}
1614
</h2>
17-
18-
<p>
19-
Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris
20-
condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod.
21-
Donec sed odio dui.
22-
</p>
23-
15+
<p> {{ctrl.col2text}}</p>
2416
<p>
2517
<a class="btn" href="#">View details</a>
2618
</p>
2719
</div>
2820
<div class="col-md-4 column">
2921
<h2>
30-
Heading
22+
{{ctrl.col3heading}}
3123
</h2>
32-
33-
<p>
34-
Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris
35-
condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod.
36-
Donec sed odio dui.
37-
</p>
38-
24+
<p> {{ctrl.col3text}}</p>
3925
<p>
4026
<a class="btn" href="#">View details</a>
4127
</p>

0 commit comments

Comments
 (0)