Skip to content

Commit 174d112

Browse files
committed
first crud
1 parent affcc39 commit 174d112

File tree

10 files changed

+144
-9
lines changed

10 files changed

+144
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ node_modules
2626

2727
# Users Environment Variables
2828
.lock-wscript
29+
30+
www/

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ wp-api-angularjs
33

44

55
```
6-
sudo npm install webpack -g
6+
sudo npm install webpack webpack-dev-server -g
7+
npm install
8+
webpack-dev-server
79
```
10+
11+
Open ```http://localhost:8080/test.html```

lib/Rest/Posts.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = angular
2+
.module('wp-api-angularjs.services.posts', [])
3+
.factory('$wpApiPosts', function($log, Restangular) {
4+
return Restangular.service('posts');
5+
});

lib/Rest/Services.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = angular.module('wp-api-angularjs.services', [
2+
require('./Posts').name
3+
]);

lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = angular.module('wp-api-angularjs', [
2+
'restangular',
3+
require('./Rest/Services').name
4+
]).config(function(RestangularProvider) {
5+
RestangularProvider.setBaseUrl('http://shprinkone.julienrenaux.fr/wp-json');
6+
RestangularProvider.setRestangularFields({
7+
id: "ID"
8+
});
9+
});

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"devDependencies": {
2525
"angular": "^1.3.6",
2626
"angularjs": "0.0.1",
27-
"restangular": "^1.4.0"
27+
"exports-loader": "^0.6.2",
28+
"expose-loader": "^0.6.0",
29+
"html-webpack-plugin": "^1.1.0",
30+
"path": "^0.4.9",
31+
"restangular": "^1.4.0",
32+
"underscore": "^1.7.0"
2833
}
2934
}

test/MainController.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = angular.module('wp-api-angularjs.test.controllers', [])
2+
.controller('MainController', function($scope, $log, $wpApiPosts) {
3+
$log.info('Main Controller');
4+
5+
$scope.getPosts = getPosts;
6+
$scope.getPost = getPost;
7+
$scope.createPost = createPost;
8+
$scope.updatePost = updatePost;
9+
$scope.removePost = removePost;
10+
11+
function getPosts() {
12+
$log.info('getPosts');
13+
$wpApiPosts.getList().then(function(posts) {
14+
$scope.getPostsStatus = 'OK';
15+
}).catch(function() {
16+
$scope.getPostsStatus = 'KO';
17+
});
18+
}
19+
20+
function getPost() {
21+
$log.info('getPost');
22+
$wpApiPosts.one(188).get().then(function(post) {
23+
$scope.getPostStatus = 'OK';
24+
}).catch(function() {
25+
$scope.getPostStatus = 'KO';
26+
});
27+
}
28+
29+
function createPost() {
30+
$log.info('createPost');
31+
$wpApiPosts.post({
32+
title: 'test',
33+
content_raw: 'test content'
34+
}).then(function(post) {
35+
$scope.createPostStatus = 'OK';
36+
}).catch(function() {
37+
$scope.createPostStatus = 'KO';
38+
});
39+
}
40+
41+
function updatePost() {
42+
$log.info('updatePost');
43+
$wpApiPosts.one(188).get().then(function(post) {
44+
post.title = 'new title';
45+
return post.put();
46+
}).then(function(post) {
47+
$scope.updatePostStatus = 'OK';
48+
}).catch(function() {
49+
$scope.updatePostStatus = 'KO';
50+
});
51+
}
52+
53+
function removePost() {
54+
$log.info('removePost');
55+
$wpApiPosts.one(188).get().then(function(post) {
56+
return post.remove();
57+
}).then(function(post) {
58+
$scope.removePostStatus = 'OK';
59+
}).catch(function() {
60+
$scope.removePostStatus = 'KO';
61+
});
62+
}
63+
});

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
require('angular');
2+
require('expose?_!underscore');
23
require('restangular');
3-
require('../lib/index.js'); // import the lib
4+
5+
module.exports = angular.module('wp-api-angularjs.test', [
6+
require('../lib/index.js').name, // import the lib
7+
require('./MainController').name
8+
])
9+
.run(function($log) {
10+
$log.info('test running');
11+
});

test/test.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
6+
<title>Webpack App</title>
7+
</head>
8+
9+
<body ng-app="wp-api-angularjs.test" ng-controller="MainController">
10+
<h2>Posts</h2>
11+
<button ng-click="getPosts()">Get list<small>{{::getPostsStatus}}<small></button>
12+
<br/>
13+
<button ng-click="getPost()">Get one <small>{{::getPostStatus}}<small></button>
14+
<br/>
15+
<button ng-click="createPost()">Create<small>{{::createPostStatus}}<small></button>
16+
<br/>
17+
<button ng-click="updatePost()">Update<small>{{::updatePostStatus}}<small></button>
18+
<br/>
19+
<button ng-click="removePost()">Remove<small>{{::removePostStatus}}<small></button>
20+
<hr/>
21+
<h2>Posts</h2>
22+
<hr/>
23+
<script src="{%=o.htmlWebpackPlugin.assets.main%}"></script>
24+
</body>
25+
26+
</html>

webpack.config.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
var path = require('path'),
2+
testPath = path.join(__dirname, 'test'),
3+
wwwPath = path.join(__dirname, 'www'),
4+
HtmlWebpackPlugin = require('html-webpack-plugin');
5+
16
module.exports = {
2-
entry: "./test/index.js",
7+
entry: path.join(testPath, 'index.js'),
38
output: {
4-
path: __dirname,
5-
filename: "bundle.js"
9+
path: wwwPath,
10+
filename: 'test.js'
611
},
712
module: {
813
loaders: [{
9-
test: /\.css$/,
10-
loader: "style!css"
14+
test: /[\/]angular\.js$/,
15+
loader: 'expose?angular!exports?window.angular'
1116
}]
12-
}
17+
},
18+
plugins: [new HtmlWebpackPlugin({
19+
filename: 'test.html',
20+
title: 'wp-api-angularjs',
21+
template: path.join(testPath, 'test.html')
22+
})]
1323
};

0 commit comments

Comments
 (0)