Skip to content

Commit

Permalink
Refactor angularjs maven archetype to have a separate namespace for d…
Browse files Browse the repository at this point in the history
…ata action and DataService with promises
  • Loading branch information
jogep committed May 1, 2015
1 parent e990fbb commit e3682f9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
* specific language governing permissions and limitations
* under the License.
*/
package ${package}.actions;
package ${package}.actions.data;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Result;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,11 +36,13 @@
public class ProjectsAction extends ActionSupport {

private static final long serialVersionUID = 9037336532369476225L;
private static final Logger log = LogManager.getLogger(ProjectsAction.class);

private List<String> projectNames = new ArrayList<String>();
private List<String> projectNames;

public String execute() throws Exception {

projectNames = new ArrayList<String>();
projectNames.add("Apache Struts");
projectNames.add("Apache Log4j");
projectNames.add("Apache Tomcat");
Expand All @@ -58,6 +63,8 @@ public String execute() throws Exception {
projectNames.add("Apache Deltaspike");
projectNames.add("Apache Cordova");

log.debug("Return {} Apache projects", projectNames.size());

return SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

<constant name="struts.convention.default.parent.package" value="angularstruts"/>
<package name="angularstruts" extends="json-default"></package>
<package name="data" extends="angularstruts" namespace="/data"></package>

</struts>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<script src="<s:url value="js/lib/angular/angular-route.min.js" />"></script>
<script src="<s:url value="js/bootstrap.js" />"></script>
<script src="<s:url value="js/directives.js" />"></script>
<script src="<s:url value="js/services.js" />"></script>
<script src="<s:url value="js/controllers.js" />"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ angularStrutsApp.controller('HomeController', function ($scope) {
$scope.name = "Sunshine";
});

angularStrutsApp.controller('ApacheProjectsController', function ($scope, $http) {
angularStrutsApp.controller('ApacheProjectsController', function ($scope, $http, DataService) {
this.init = function() {
$http({method: 'GET', url: 'projects'}).
success(function(data) {
$scope.projects = data.projectNames;
}).
error(function(data, status, headers, config) {
alert("Could not receive project names");
});
DataService.getProjects().then(function(data) {
$scope.projects = data.data.projectNames;
}, function(data) {
console.log('Could not receive project names.')
});
};

this.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
angularStrutsApp.factory('DataService', ['$http', '$q', function($http, $q) {

var DataService = {
urls : {
projects : "data/projects"
}
};

DataService._request = function(url, method, model){
if(!method) {
method = 'GET';
}
var def = $q.defer();
if(method === 'GET') {
return $http.get(url).success(function(data) {
DataService.data = data;
def.resolve(data);
}).error(function() {
def.reject("Failed to get data");
});
} else if(method === 'POST'){
$http.post(url, model).success(function(data) {
DataService.data = data;
def.resolve(data);
}).error(function() {
def.reject("Failed to post data");
});
}
return def.promise;
};

DataService.getProjects = function () {
return this._request(this.urls.projects);
};

return DataService;
}]);

0 comments on commit e3682f9

Please sign in to comment.