2016-03-02 77 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