如何正确设置Grails更新控制器以避免空指针异常?

时间:2015-05-20 12:30:59

标签: angularjs grails

我的Grails域类正是你所期望的,只是一些可以为空的变量而另一些不是。

这是我的控制器文件:

  

包   服务器
  import grails.converters.JSON
  class TodoListController {

def addItem() {
    def newItem = new Item(request.JSON)
    newItem.save(flush: true)
    render newItem as JSON
}

def index(){
    render Item.list() as JSON
}

def updateList() {
    def newItem = Item.findById(request.JSON.id)
    newItem.name = request.JSON.name
    newItem.type = request.JSON.type
    newItem.priority = request.JSON.priority
    newItem.completed = request.JSON.completed
    newItem.comments = request.JSON.comments
    newItem.creator = request.JSON.creator
    newItem.assignedTo = request.JSON.assignedTo
    newItem.save(flush: true)
    render newItem as JSON
} }

我的Angular脚本:

/*------------------------------------------------->
<!-------------------TO DO LIST JS----------------->
<!-------------------------------------------------*/

angular.module('todoApp', ['ngResource'])       // Module for todolist
.controller('todoList', ['$scope', '$resource', function($scope, $resource) {
        var serverList = $resource('http://localhost:8080/server/todoList/:command');   // Allow commands to be sent to grails app

    $scope.items = serverList.query({command: 'index'});

        $scope.usernames = [];

    $scope.thing = "";  // Entering task default entry

        $scope.priorityOptions =        // List of priority options, name is used for listing options, id for priority color/sort
                [{label: "Top Priority", id: 1},
                 {label: "Mid Priority", id: 2},
                 {label: "Low Priority", id: 3}];

        $scope.worktypeOptions = // Different types of work options
                [{label: "Work", id:1},
                 {label: "Personal", id:2}];

        $scope.addusername = function(){
                $scope.usernames.push($scope.username);
        };

        $scope.ToDoView = function(){
        for(var i = 0; i < $scope.items.length; i++) {
            if(($scope.items[i].creator != $scope.username) && ($scope.items[i].assignedTo != $scope.username)) {
                $scope.items.splice(i, 1);
                $scope.ToDoView();
                break;
            }
        }
        };

    $scope.add = function(){    // Function used to add a value to list. Value has name, type, and priority.
                if($scope.thing){      
                        $scope.items.push(
                                {name: $scope.thing,
                                 type: $scope.worktypeSelection,
                                 priority: $scope.prioritySelection,
                                 completed: false,
                                 comments: '',
                                 creator: $scope.username,
                                 assignedTo: $scope.assign
                        });
                        $scope.thing = '';
            $scope.items[$scope.items.length - 1] = serverList.save({command: 'addItem'}, $scope.items[$scope.items.length - 1]);
                }
                else{
                        alert("Empty task is not allowed!");
                }
        };


        $scope.updateList = function() {
        angular.forEach($scope.items, function (item) {
            serverList.save({command: 'updateList'}, item);
        });
    };

    $scope.clear_all = function(){      // Sets "item" array to empty; deletes all.
        $scope.items = [];
    };

        $scope.clear_selected = function(){
                var itemstmp = $scope.items;    // Keep items
                $scope.items = [];      // Clear list
                angular.forEach(itemstmp, function(itemName){
                        if(!itemName.completed){
                                $scope.items.push(itemName);   
                        }
                });
        };
}]);

现在我的HTML设置为每当用户添加列表中的新项时,就会调用JS中的add函数,它应该在我的groovy文件中调用addItem控制器。我不确定这是否正常。

然后,当用户单击“保存列表”按钮时,它应该更新服务器的JSON列表。这里的问题是我从Grails得到一个Null Pointer Exception,因为“无法为null对象定义名称”。我理解这里发生了什么(没有检测到对象被更新),但我不明白什么是错的。

0 个答案:

没有答案