新しいオブジェクトの作成をトリガーできるように、コレクションへの変更を検出しようとしています。たとえば、インベントリには多くのアイテムがあります。アイテムには名前と数量があります。ダーティチェックを使用して、アイテムの追加/削除を検出できます。
//Detecting add/change
inventoryInstance.items.isDirty()
関連するアイテム オブジェクト (名前、数量) の変更と、アイテムの注文変更を特定するのが困難です。isDirty() は、これらの変更を識別していないようです。私は Grails 1.3.7 を使用しており、HTML フォームとインデックス付きフィールド名 (items[0].name、items[0].quantity) を介して grails データバインディングを使用しています。誰かが提案された解決策または代替アプローチを持っていますか?
ドメイン クラスの例:
class Inventory {
List items = new ArrayList()
static hasMany = [items:Item]
static constraints = {
items(size:0..10)
}
static mapping = {
items cascade: 'all-delete-orphan'
}
}
class Item {
String name
Integer quantity = 1
Boolean deleted = false
static transients = ['deleted']
static belongsTo = Inventory
static constraints = {
name blank:false
quantity size:1..100
}
}
コントローラ
def inventoryInstance = Inventory.get(params.id)
if( inventoryInstance ) {
inventoryInstance.properties = params
// Check for add/removes, this works.
def isDirty = inventoryInstance.items.isDirty()
if(!isDirty) {
// Check items in list for changes, does not work..
inventoryInstance.items.each { item ->
if(item.dirtyPropertyNames) isDirty = true
}
}
if(isDirty) {
//do something...
}
inventoryInstance.save(flush:true)
}