1
+ "use strict" ;
2
+ var reang ;
3
+ var $ = jQuery ;
4
+ reang = angular . module ( 'reang' , [ 'ngResource' ] )
5
+ . filter ( 'to_trusted' , [ '$sce' , function ( $sce ) {
6
+ return function ( text ) {
7
+ return $sce . trustAsHtml ( text ) ;
8
+ } ;
9
+ } ] )
10
+ . factory ( 'Posts' , function ( $resource ) {
11
+ return $resource ( ajaxInfo . json_url + 'posts/:ID?_wp_json_nonce=' + ajaxInfo . nonce , {
12
+ ID : '@ID'
13
+ } , {
14
+ 'update' : { method :'PUT' }
15
+ } ) ;
16
+ } )
17
+ . controller ( 'reang_controller' , [ '$rootScope' , '$scope' , 'Posts' , function ( $rootScope , $scope , Posts ) {
18
+ $scope . getPosts = function ( ) {
19
+ Posts . query ( { } , function ( res ) {
20
+ $scope . posts = res ;
21
+ } ) ;
22
+ }
23
+
24
+ $scope . getPosts ( ) ;
25
+
26
+
27
+ $ ( 'body' ) . on ( 'click' , '.edit_post' , function ( e ) {
28
+ var post_id = $ ( this ) . data ( 'id' ) ;
29
+ console . log ( post_id ) ;
30
+
31
+ Posts . get ( { ID :post_id } , function ( res ) {
32
+ $scope . editPost = res ;
33
+ } )
34
+
35
+ } )
36
+
37
+ $scope . savePost = function ( ) {
38
+ console . log ( 'saving..' , $scope . editPost ) ;
39
+ $scope . editPost . content_raw = $scope . editPost . content ;
40
+ Posts . update ( $scope . editPost , function ( res ) {
41
+ $scope . getPosts ( ) ;
42
+ $ ( '#editPost' ) . modal ( 'hide' ) ;
43
+ } )
44
+ }
45
+
46
+ } ] )
47
+ . directive ( 'reactposts' , function ( $rootScope ) {
48
+ return {
49
+ restrict : 'E' ,
50
+ scope : { data : '=' , id : '@' } ,
51
+ link : function ( $scope , elm , attrs ) {
52
+ $scope . $watch ( 'data' , function ( n , o ) {
53
+ if ( n && n . length ) {
54
+ $rootScope . react_app = React . render (
55
+ React . createElement ( APP , { data :$scope . data } ) ,
56
+ elm [ 0 ]
57
+ )
58
+ }
59
+ } ) ;
60
+ }
61
+ }
62
+ } )
63
+ . controller ( 'editPostCtrl' , [ '$rootScope' , '$scope' , 'Posts' , function ( $rootScope , $scope , Posts ) {
64
+ console . log ( 'editing..' ) ;
65
+
66
+ $ ( 'body' ) . on ( 'click' , '.edit_post' , function ( e ) {
67
+ var post_id = $ ( this ) . data ( 'id' ) ;
68
+ console . log ( post_id ) ;
69
+
70
+ Posts . get ( { ID :post_id } , function ( res ) {
71
+ $scope . editPost = res ;
72
+ } )
73
+
74
+ } )
75
+
76
+ $scope . savePost = function ( ) {
77
+ console . log ( 'saving..' , $scope . editPost ) ;
78
+ $scope . editPost . content_raw = $scope . editPost . content ;
79
+ Posts . update ( $scope . editPost , function ( res ) {
80
+ Posts . query ( { } , function ( res ) {
81
+ $rootScope . react_app . data = res ;
82
+ $ ( '#editPost' ) . modal ( 'hide' ) ;
83
+ console . log ( $rootScope . react_app . data ) ;
84
+ } )
85
+ } )
86
+ }
87
+ } ] ) ;
88
+ var $ = jQuery ;
0 commit comments