Skip to content

Commit 247d191

Browse files
committed
Add Webpack sample.
This sample uses the same example code as the ASP.NET MVC 4 sample, but also demonstrates how to use Webpack to bundle the code and export components for server-side rendering.
1 parent 5b4b52a commit 247d191

22 files changed

+764
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ src/**/*.nuspec
55
!src/template.nuspec
66
site/jekyll/_site
77
src/React.Sample.Cassette/cassette-cache
8-
src/React.Sample.Mvc4/ClearScript.V8
8+
src/React.Sample.*/ClearScript.V8
9+
src/React.Sample.Webpack/build
910
*.generated.js
1011
*.generated.js.map
1112

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(React.Sample.Webpack.ReactConfig), "Configure")]
11+
12+
namespace React.Sample.Webpack
13+
{
14+
public static class ReactConfig
15+
{
16+
public static void Configure()
17+
{
18+
ReactSiteConfiguration.Configuration
19+
.AddScript("~/build/server.bundle.js");
20+
}
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2014-2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
body {
11+
font-family: Calibri, Verdana, sans-serif;
12+
}
13+
14+
.commentList {
15+
list-style-type: none;
16+
margin: 0;
17+
padding: 0;
18+
}
19+
20+
.commentList li {
21+
border-bottom: 1px solid #999;
22+
padding: 0.5em 0;
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
// All JavaScript in here will be loaded client-side
11+
12+
// Expose components globally so ReactJS.NET can use them
13+
var Components = require('expose?Components!./components');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
var React = require('react');
11+
12+
var Avatar = React.createClass({
13+
propTypes: {
14+
author: React.PropTypes.object.isRequired
15+
},
16+
render() {
17+
return (
18+
<img
19+
src={this.getPhotoUrl(this.props.author)}
20+
alt={'Photo of ' + this.props.author.Name}
21+
width={50}
22+
height={50}
23+
className="commentPhoto"
24+
/>
25+
);
26+
},
27+
getPhotoUrl(author) {
28+
return 'http://graph.facebook.com/' + author.Facebook + '/picture';
29+
}
30+
});
31+
32+
module.exports = Avatar;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
var Avatar = require('./Avatar');
11+
var React = require('react');
12+
13+
var Comment = React.createClass({
14+
propTypes: {
15+
author: React.PropTypes.object.isRequired
16+
},
17+
render() {
18+
return (
19+
<li>
20+
<Avatar author={this.props.author} />
21+
<strong>{this.props.author.Name}</strong>{': '}
22+
{this.props.children}
23+
</li>
24+
);
25+
}
26+
});
27+
28+
module.exports = Comment;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
var Comment = require('./Comment');
11+
var React = require('react');
12+
13+
var CommentsBox = React.createClass({
14+
propTypes: {
15+
initialComments: React.PropTypes.array.isRequired
16+
},
17+
getInitialState() {
18+
return {
19+
comments: this.props.initialComments,
20+
page: 1,
21+
hasMore: true,
22+
loadingMore: false
23+
};
24+
},
25+
loadMoreClicked(evt) {
26+
var nextPage = this.state.page + 1;
27+
this.setState({
28+
page: nextPage,
29+
loadingMore: true
30+
});
31+
32+
var url = evt.target.href;
33+
var xhr = new XMLHttpRequest();
34+
xhr.open('GET', url, true);
35+
xhr.onload = () => {
36+
var data = JSON.parse(xhr.responseText);
37+
this.setState({
38+
comments: this.state.comments.concat(data.comments),
39+
hasMore: data.hasMore,
40+
loadingMore: false
41+
});
42+
};
43+
xhr.send();
44+
return false;
45+
},
46+
render() {
47+
var commentNodes = this.state.comments.map(comment =>
48+
<Comment author={comment.Author}>{comment.Text}</Comment>
49+
);
50+
51+
return (
52+
<div className="comments">
53+
<h1>Comments</h1>
54+
<ol className="commentList">
55+
{commentNodes}
56+
</ol>
57+
{this.renderMoreLink()}
58+
</div>
59+
);
60+
},
61+
renderMoreLink() {
62+
if (this.state.loadingMore) {
63+
return <em>Loading...</em>;
64+
} else if (this.state.hasMore) {
65+
return (
66+
<a href={'comments/page-' + (this.state.page + 1)} onClick={this.loadMoreClicked}>
67+
Load More
68+
</a>
69+
);
70+
} else {
71+
return <em>No more comments</em>;
72+
}
73+
}
74+
});
75+
76+
module.exports = CommentsBox;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
module.exports = {
11+
Avatar: require('./Avatar'),
12+
Comment: require('./Comment'),
13+
CommentsBox: require('./CommentsBox')
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
// All JavaScript in here will be loaded server-side
11+
12+
// Expose components globally so ReactJS.NET can use them
13+
var Components = require('expose?Components!./components');

src/React.Sample.Webpack/Global.asax

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%@ Application Codebehind="Global.asax.cs" Inherits="React.Sample.Webpack.MvcApplication" Language="C#" %>

0 commit comments

Comments
 (0)