Skip to content

Commit a2909c3

Browse files
Roy SivanRoy Sivan
authored andcommitted
react and angular working together in directive
1 parent 56a93ec commit a2909c3

File tree

9 files changed

+48680
-20077
lines changed

9 files changed

+48680
-20077
lines changed

assets/js/angular_app.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,20 @@ reang = angular.module('reang', ['ngResource'])
1414
Posts.query({}, function(res){
1515
$scope.posts = res;
1616
});
17-
}]);
17+
}])
18+
.directive('reactposts', function() {
19+
return {
20+
restrict: 'E',
21+
scope: { data: '=', id: '@' },
22+
link: function($scope,elm,attrs) {
23+
$scope.$watch('data', function(n,o){
24+
if( n && n.length ) {
25+
React.render(
26+
React.createElement(APP, {data:$scope.data}),
27+
elm[0]
28+
)
29+
}
30+
})
31+
}
32+
}
33+
});

assets/js/react_app.jsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,10 @@ var Post = React.createClass({
1616

1717
var App = React.createClass({
1818

19-
getposts: function() {
20-
console.log('getting posts..');
21-
$.ajax({
22-
url: 'http://dclient.dev/wp-json/posts',
23-
dataType: 'json',
24-
method: 'GET',
25-
success: function(res){
26-
this.setState({data:res})
27-
}.bind(this)
28-
});
29-
},
30-
31-
getInitialState: function() {
32-
return { data: [{ title: 'test', content: '<p>test</p>'}] }
33-
},
34-
35-
componentWillMount: function() {
36-
this.getposts();
19+
componentWillReceiveProps: function( nextProps ) {
20+
if(nextProps.data){
21+
this.state.data = nextProps.data;
22+
}
3723
},
3824

3925
render: function() {
@@ -51,4 +37,4 @@ var App = React.createClass({
5137
}
5238
});
5339

54-
React.render(<App/>, document.getElementById( 'test_react' ) );
40+
//React.render(<App/>, document.getElementById( 'test_react' ) );

assets/js/react_app.jsx.backup

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var React = require('react');
2+
var $ = jQuery;
3+
4+
var Post = React.createClass({
5+
render: function(){
6+
function post_content(html){ return {__html: html } }
7+
return (
8+
<article>
9+
<h1>{this.props.post.title}</h1>
10+
<div dangerouslySetInnerHTML={{__html: this.props.post.content}}></div>
11+
</article>
12+
)
13+
}
14+
})
15+
16+
17+
var App = React.createClass({
18+
19+
getposts: function() {
20+
console.log('getting posts..');
21+
$.ajax({
22+
url: 'http://dclient.dev/wp-json/posts',
23+
dataType: 'json',
24+
method: 'GET',
25+
success: function(res){
26+
this.setState({data:res})
27+
}.bind(this)
28+
});
29+
},
30+
31+
getInitialState: function() {
32+
return { data: [{ title: 'test', content: '<p>test</p>'}] }
33+
},
34+
35+
componentWillMount: function() {
36+
this.getposts();
37+
},
38+
39+
render: function() {
40+
if(this.state.data.length) {
41+
return (
42+
<div>
43+
{this.state.data.map(function(post){
44+
return (
45+
<Post post={post}></Post>
46+
)
47+
})}
48+
</div>
49+
)
50+
}
51+
}
52+
});
53+
54+
React.render(<App/>, document.getElementById( 'test_react' ) );

0 commit comments

Comments
 (0)