2014-05-01 21 views
0

我相信我看到有人寫過關於從模型對象中獲取內存中對象以減少數據庫訪問的方法。可以請一些機構建議或分享我的文件?Sailjs /水線 - 有什麼辦法可以獲得先前提取的物體?

我想有這樣的事情:

ModelA.create({...}).populate('modelBs').exec(function(err, instanceA) { 
    // Do some nested things here, for example: 
    instanceA.prop1 = someNewValue; 
    instanceA.save(function(err, instanceAModified) { 
     // I want to call some method from instanceAModified, to get all 'modelB' objects here, without accessing the DB once more. 
    }); 
}); 

預先感謝您!

回答

0

我相信在Waterline的最新版本中,.save()將重新填充其回調中的任何關聯。但不管怎樣,你總是可以將它們保存在局部變量中。假設你的意思find而不是create以上(因爲populate不會做任何create):

ModelA.find({...}).populate('modelBs').exec(function(err, instanceA) { 
    // Do some nested things here, for example: 
    instanceA.prop1 = someNewValue; 
    var modelBs = instanceA.modelBs; 
    instanceA.save(function(err, instanceAModified) { 
     // modelBs will still be available here. 
    }); 
}); 
+0

明白了。非常感謝! – Ducky

相關問題