Skip to content

Commit b6f052c

Browse files
committed
loading indicator
1 parent 87c576d commit b6f052c

File tree

9 files changed

+87
-2
lines changed

9 files changed

+87
-2
lines changed

ClientApp/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import App from './components/App.vue';
33
import store from './vuex/store.js';
44
import router from './router';
55

6+
67
const app = new Vue({
78
router,
89
store,

ClientApp/components/Messages.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<script>
99
import {mapGetters, mapActions} from 'vuex';
1010
import Message from './Message.vue';
11+
1112
export default {
1213
components: { Message },
1314
computed: mapGetters(['messages', 'lastFetchedMessageDate']),

ClientApp/vuex/actions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import axios from 'axios';
2+
import NProgress from 'nprogress';
3+
import 'nprogress/nprogress.css'; // <- this line is required
24

35
export const fetchInitialMessages = ({ commit }) => {
6+
NProgress.start();
47
axios.get('initialMessages').then(response => {
58
commit("INITIAL_MESSAGES", response.data);
9+
NProgress.done();
610
}).catch(err => {
711
console.log(err);
12+
NProgress.done();
813
});
914
};
1015

1116
export const fetchMessages = ({ commit }, lastFetchedMessageDate) => {
17+
NProgress.start();
1218
axios.get('/fetchMessages?lastFetchedMessageDate='+lastFetchedMessageDate)
1319
.then(response => {
1420
commit("FETCH_MESSAGES", response.data);
21+
NProgress.done();
1522
}).catch(err => {
1623
console.log(err);
24+
NProgress.done();
1725
});
1826
};

Controllers/HomeController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using System.Threading;
67
using Microsoft.AspNetCore.Mvc;
78

89
namespace vdn.Controllers
@@ -16,6 +17,9 @@ public IActionResult Index()
1617

1718
[Route("initialMessages")]
1819
public JsonResult initialMessages(){
20+
//Added to simulate initial loading from remote server
21+
Thread.Sleep(2000);
22+
1923
var initialMessages = FakeMessageStore.FakeMessages.OrderByDescending(m => m.Date)
2024
.Take(2);
2125

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ This repository is a clone from Stu Ratcliffe [Server rendering Vue.js applicati
22

33
Following his articile and trying to have some write up. (e.g. using latest packages in package.json)
44

5+
## Install and run:
6+
if Webpack is not installed yet:
7+
`npm install -g webpack
8+
9+
`dotnet restore
10+
npm install
11+
webpack
12+
dotnet run`
513
## Packages used:
614
- lodash <- similiar to numpy in Python, utilies library for manipulating array/object.
715
- axios <- Promise based HTTP client for the browser and Node.js, think of $.ajax() if you come from jQuery world
@@ -26,4 +34,29 @@ Following his articile and trying to have some write up. (e.g. using latest pack
2634
7. Move back to ClientApp folder
2735
8. Modify app.js
2836
9. Move back to .
29-
10. Modify Startup.cs
37+
10. Modify Startup.cs
38+
### Loading indicator:
39+
Here we try to modify the implementation order different from the original post.
40+
We are going to add the loading indicator before implementing the Server Side Rendering.
41+
To simulate timely API call form remote server, we add the following line in HomeController.cs:
42+
`public JsonResult initialMessages(){
43+
//Added to simulate initial loading from remote server
44+
Thread.Sleep(2000);
45+
...
46+
}`
47+
48+
1. Add nprogess in package.json dependency:
49+
`"dependencies": {
50+
"vue": "^2.5.8",
51+
"vuex": "^3.0.1",
52+
"vue-router": "^3.0.1",
53+
"lodash": "^4.17.4",
54+
"axios": "^0.17.1",
55+
"nprogress": "^0.2.0"
56+
},`
57+
2. Add style-loader and css-loader to webpack.config:
58+
`{
59+
test: /\.css$/,
60+
loader: "style-loader!css-loader"
61+
}`
62+
3. Modify ClientApp/vuex/actions.js, add `NProgress.start()` and `NProgress.done()` before and after axios remote call.

Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
4747
app.UseMvc(routes =>
4848
{
4949
routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index"});
50+
5051
routes.MapRoute(
5152
name: "default",
5253
template: "{controller=Home}/{action=Index}/{id?}");

package.before_loading_indicator.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "vdn",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Ferry To",
10+
"license": "MIT",
11+
"private": true,
12+
"dependencies": {
13+
"vue": "^2.5.8",
14+
"vuex": "^3.0.1",
15+
"vue-router": "^3.0.1",
16+
"lodash": "^4.17.4",
17+
"axios": "^0.17.1"
18+
},
19+
"devDependencies": {
20+
"babel-core": "^6.26.0",
21+
"babel-loader": "^7.1.2",
22+
"babel-preset-es2015": "^6.24.1",
23+
"babel-preset-stage-2": "^6.24.1",
24+
"vue-loader": "^13.5.0",
25+
"vue-template-compiler": "^2.5.8",
26+
"css-loader": "^0.28.7",
27+
"aspnet-webpack": "^2.0.1",
28+
"webpack": "^3.8.1",
29+
"webpack-hot-middleware": "^2.20.0"
30+
}
31+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"vuex": "^3.0.1",
1515
"vue-router": "^3.0.1",
1616
"lodash": "^4.17.4",
17-
"axios": "^0.17.1"
17+
"axios": "^0.17.1",
18+
"nprogress": "^0.2.0"
1819
},
1920
"devDependencies": {
2021
"babel-core": "^6.26.0",
@@ -24,6 +25,7 @@
2425
"vue-loader": "^13.5.0",
2526
"vue-template-compiler": "^2.5.8",
2627
"css-loader": "^0.28.7",
28+
"style-loader": "^0.19.0",
2729
"aspnet-webpack": "^2.0.1",
2830
"webpack": "^3.8.1",
2931
"webpack-hot-middleware": "^2.20.0"

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module.exports = {
1717
test: /\.js$/,
1818
loader: 'babel-loader',
1919
exclude: /node_modules/
20+
},
21+
{
22+
test: /\.css$/,
23+
loader: "style-loader!css-loader"
2024
}
2125
]
2226
}

0 commit comments

Comments
 (0)