【问题标题】:How can I update an existing JSON object's parameters in Grails?如何在 Grails 中更新现有 JSON 对象的参数?
【发布时间】:2015-05-27 13:50:26
【问题描述】:

我正在制作一个待办事项列表。首次输入项目并将其添加到列表时,服务器运行良好。它接受用户选择的参数并将它们传递到服务器上的列表中,该列表可以通过渲染 Item.list() 来查看,如下所示:

[{"class":"server.Item","id":1,"assignedTo":"User 1","comments":null,"completed":false,"creator":"User 1","name":"Task 1","priority":"1","type":"Personal"},
{"class":"server.Item","id":2,"assignedTo":"User 2","comments":null,"completed":false,"creator":"User 2","name":"Er","priority":"3","type":"Work"},
{"class":"server.Item","id":3,"assignedTo":"User 1","comments":null,"completed":false,"creator":"User 2","name":"Ga","priority":"1","type":"Work"}]

现在,用户可以选择稍后编辑任务。在客户端这工作正常,但我需要用户能够保存新的更新任务。

这是我当前的更新功能:

def updateList() {
    def newItem = Item.findById(request.JSON.id)

    newItem.assignedTo = request.JSON.assignedTo
    newItem.comments = request.JSON.comments
    newItem.completed = request.JSON.completed
    newItem.creator = request.JSON.creator
    newItem.name = request.JSON.name
    newItem.priority = request.JSON.priority
    newItem.type = request.JSON.type
    newItem.save(flush: true)
    render newItem as JSON
}

但是,这不起作用。我得到一个空指针异常,上面写着“无法在空对象上设置属性”assignedTo。我假设 findById 请求没有为 JSON 对象获取任何内容,因此没有对象可以分配值,但是我没有'不知道问题是什么考虑到这些项目实际上被放入了 Item.list()。

这是在客户端使用以下JS函数调用的:

$scope.updateList = function() {
    angular.forEach($scope.items, function (item) {
       // serverList.save({command: 'updateList'}, item);
        $http.post('http://localhost:8080/server/todoList/updateList', item)
            .success(function(response) {})
            .error(function(response) {alert("Failed to update");});
    });
};

【问题讨论】:

  • 无关 - 避免使用findById 并改用get。它不会影响这个问题,因为它的作用基本相同,但它们的缓存方式非常不同,get 的性能会更好。

标签: json angularjs grails


【解决方案1】:

这可能取决于您的 Grails 版本,但您应该可以这样做:

def update(Item item) {
  if (!item) {
    // return a 404
  } else {
    // you should really use a service and not save
    // in the controller
    itemService.update(item)
    respond item
  }
}

Grails 足够聪明地查找该项目,因为 JSON 参数中有一个 ID,并正确填充该对象。

【讨论】:

  • 如果您不介意,itemService 服务会是什么样子?
  • 很可能只是item.save(),除非你有更多的业务逻辑来解决它。但使用服务层是首选做法。所以这只是一个建议。
  • 发起post请求是否传递了update(Item item)中声明的变量?如果没有,我该如何申报?
  • 在此处阅读名为“数据绑定和操作参数”的部分:grails.github.io/grails-doc/2.5.0/guide/…
【解决方案2】:

对于可能需要以基本方式执行此操作的其他任何人来说,我所做的工作是在单击“更新列表”时清除列表,然后读回当前的值在客户端列表中。

Grails:

def clearList() {
    Item.executeUpdate('delete from Item')
    render Item.list()
}

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

Javascript:

$scope.updateList = function() {    // Update list on the server
    serverList.get({command: 'clearList'});
    angular.forEach($scope.items, function (item) {
        serverList.save({command: 'updateList'}, item);
    });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2023-03-05
    • 2020-05-02
    • 2023-02-13
    • 1970-01-01
    相关资源
    最近更新 更多