0

圣杯 2.2.0

我正在探索 grails 和 ajax,也许我是一个狂热的 ajax 适配器,但我真的认为它改变了游戏规则,所以我要先入为主。

数据模型是主要细节 (1:n)。客户端中的表格显示了主属性列表的一部分。单击后,线条折叠打开,显示所有细节(一个属性域)和一个向展开的母版添加新细节的选项。好吧,开通这条线电话

def show (Long id) {
    def product = Product.get(id)
    log.info('show ' + (request.xhr ? 'xerhr' : 'normal') + "product is ${product.title}")
    def c = Attribute.createCriteria();
    def attributeTypes = LookupValue.findByType('m_attr') //list of values for dropdown menu.
    def result = c {
        eq("movie", product)
    }
    for (attr in result) {
        log.info('value: ${attr.value}')
    }
    render ([template: "attributes", model:[focus:params.id
        , attributeList: result, attributeTypes: attributeTypes]])
}

属性模板显示所有相关属性(详细信息)和末尾的添加选项,您可以在其中输入新值并从下拉菜单中选择值。客户端确实向控制器的 saveAttribute 方法执行 ajax 回传以保存此新属性:

def saveAttribute(Long id, Long luv_id, String value) {
    def attribute = new Attribute(movie : Product.get(id)
        , label : LookupValue.get(luv_id)
        , value : value);
    attribute.save();
    show();
}

并再次调用show()以将新创建​​的属性列表呈现回客户端。现在 show() 确实渲染了所有现有的属性,但它无法检索刚刚创建的属性,而它确实最终在数据库中。当我重新加载页面时,它也会出现。我哪里错了?有了hibernate,我想我会做一个session.flush()来克服这个问题。

也许我的架构是错误的,我需要定义一个服务并引入一些事务边界?

4

1 回答 1

1
def saveAttribute(Long id, Long luv_id, String value) {
    Attribute attribute = new Attribute(movie: Product.get(id)
        , label: LookupValue.get(luv_id)
        , value: value).save(flush: true)
    forward(action: "show", id: id)
}
于 2013-01-28T03:21:45.593 回答