开发者

Grails Domain Class Dynamic List As A property

Let's say I have a doma开发者_JS百科in class called ShopCategoryPageTab. And I have a domain class called Product.

I want ShopCategoryPageTab to have a list of Products. However, this list is not static, but determined by a formula.

For example, I might want to have a "products" property which would list all products with criteria X, Y Z.

So this property/list is not entered manually by someone, it is dynamically generated. (products can be removed/added by an external applications, product properties could be changing).

Is there such a thing?


Often you call the dynamically added methods like list(), findAll(), findByFooAndBar(), etc., but you can always add your own. So in your case you'd create something like this:

class ShopCategoryPageTab {

   ...
   List findAllProductsByXYZ(x, y, z) {
      ...
   }
}

Name it however you like of course, and the implementation will be a criteria query or an HQL query via executeQuery().


Agree with @Burt above.. alternatively you can also call your methods as getXProduct() or getYProduct() and access them as fields instead of methods.

The fields would be like XProduct and YProduct. You'll have to mark these fields transients explicitly..

Hope it helps..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜