1

I'm using Graisl 3.1.1, rest-api profile. I'm trying to build a Category Tree but I haven some problems rendering the categories in JSON-Views.

I'm using json templates, one for the parent and another for the child. Basically I want to generate a json for angular something like this:

angularjs Category treet

These is my code.

Any help?

Stacktrace

//domain

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

//controller

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')
}

The problem is the categories line above, I'm not sure what is the problem or my mistake.

//_child.gson

import com.example.Category
model {
  Category child
}
json {
  name child.name
}
Ryan Heathcote
  • 836
  • 8
  • 19
Adrian Rodriguez
  • 452
  • 4
  • 19

2 Answers2

1

I'm reasonably confident you are encountering the same fresh bug (fixed in grails-views@1.0.4) that I encountered a couple weeks ago, #9720:

it would appear that the relative path isn't recognized for g.render and the fully qualified path is required even when the template is located in the same directory as the calling gson file.

Instead of:

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

prepend a slash to the template:

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

You may also need to change:

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

to:

categories g.render(template: '/category/parent', collection: categoryList ?: [], var: 'category')
Ryan Heathcote
  • 836
  • 8
  • 19
-1

I usually use in controller

import grails.converters.JSON

and then

render result as JSON

Arjang
  • 731
  • 1
  • 10
  • 19
  • 1
    thanks for your answer! but I'm looking forward to use json-views feature instead that solution. – Adrian Rodriguez Mar 02 '16 at 17:11
  • New plugin was released an hour ago. http://grails-plugins.org/#plugin/views-gradle And I am sure you have seen documentations. http://grails.github.io/grails-views/latest/ – Arjang Mar 02 '16 at 19:07