2016-03-02 76 views
1

我正在使用Graisl 3.1.1,rest-api配置文件。 我想建立一个类别树,但我没有在JSON视图中呈现类别的一些问题。Grails 3.1 JSON-视图,呈现类别树

我正在使用json模板,一个用于父级,另一个用于子级。 基本上我想生成角的东西JSON这样的:

angularjs Category treet

这是我的代码。

任何帮助?

Stacktrace

//域

class Category { 
    ObjectId id /* MongoDB */ 
    static hasMany = [categories: Category] 
    String name 
    ... 

//控制器

def directory(){ 
    def categories = Category.findAllByCategoriesIsNotNull([sort: 'name', order: 'asc']) 
    respond categories 
} 

//directory.gson

import com.example.Category 
model { 
    Iterable<Category> categoryList 
} 
json { 
    categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category') 
} 

//_parent.gson

import com.example.Category 
model { 
    Category category 
} 
json { 
    id category.id.toString() 
    name category.name 
    categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child') 
} 

问题是上面的categories行,我不确定是什么问题或我的错误。

//_child.gson

import com.example.Category 
model { 
    Category child 
} 
json { 
    name child.name 
} 

回答

1

我有理由相信你遇到相同的错误新鲜(固定在[email protected]),我遇到了几个星期前,#9720

它会出现在g.render不会识别相对路径,即使模板与调用的gson文件位于同一目录中,也需要完全限定路径。

相反的:

categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child') 

,预先准备的模板斜线:

categories g.render(template: "/category/child", collection: category.categories ?: [], var: 'child') 

您可能还需要改变:

categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category') 

到:

categories g.render(template: '/category/parent', collection: categoryList ?: [], var: 'category') 
+1

我也更新了编译“org.grails.plugins:views-json:1.0.3”来编译“org.grails.plugins:views-json:1.0 .4“谢谢。 –

-1

我通常在控制器使用

进口grails.converters.JSON

然后

渲染的结果JSON

+1

感谢您的回答!但我期待使用json-views功能来代替该解决方案。 –

+0

新插件于一小时前发布。 http://grails-plugins.org/#plugin/views-gradle我相信你已经看过文件。 http://grails.github.io/grails-views/latest/ – Arjang