Mongodb 是否可以使用集合多态性简化此代码?
我必须收集不同的角色和灵魂,它们共享许多相同的属性,并在相同的上下文中使用。这意味着每次我想读/写这些集合时,我都必须进行“类型检查”,然后重复代码两次,如下所示。有什么方法可以实现吗Mongodb 是否可以使用集合多态性简化此代码?,mongodb,meteor,Mongodb,Meteor,我必须收集不同的角色和灵魂,它们共享许多相同的属性,并在相同的上下文中使用。这意味着每次我想读/写这些集合时,我都必须进行“类型检查”,然后重复代码两次,如下所示。有什么方法可以实现吗 Polymorphic.update().. 而不是 (Pseudocode) if target.is(Character) same logic.. Character.update(same query/fields).. else same logic.. Soul.update(sam
Polymorphic.update()..
而不是
(Pseudocode)
if target.is(Character)
same logic..
Character.update(same query/fields)..
else
same logic..
Soul.update(same query/fields)..
完成下面的代码
#
# Adds a new condition instance to target
#
addCondition: (effect, target) ->
name = effect.name
maxDuration = effect.maxDuration
curDuration = if effect.curDuration then effect.curDuration else maxDuration
maxStack = effect.maxStack
curStack = if effect.curStack then effect.curStack else 1
stackable = if maxStack then true else false
if target.location <--- This is my type check, only the Character collection has a location field
character = Characters.findOne({_id: target._id, 'conditions.name': name}, { fields: {'conditions.$': 1} })
if character then existingCondition = character.conditions[0]
if existingCondition and stackable
# Apply additional stack and refresh duration
if existingCondition.curStack < existingCondition.maxStack
Characters.update({ _id: target._id, 'conditions.name': name }, { $inc: { 'conditions.$.curStack': 1 }, $set: { 'conditions.$.curDuration': maxDuration } })
else
Characters.update({ _id: target._id, 'conditions.name': name }, { $set: { 'conditions.$.curDuration': maxDuration } })
else if existingCondition and !stackable
Characters.update({ _id: target._id, 'conditions.name': name }, { $set: { 'conditions.$.curDuration': maxDuration } })
else
effect = _.extend(effect, {curDuration: curDuration, curStack: curStack})
Characters.update(_id: target._id, {$addToSet: { conditions: effect }})
else
soul = Souls.findOne({_id: target._id, 'conditions.name': name}, { fields: {'conditions.$': 1} })
if soul then existingCondition = soul.conditions[0]
if existingCondition and stackable
# Apply additional stack and refresh duration
if existingCondition.curStack < existingCondition.maxStack
Souls.update({ _id: target._id, 'conditions.name': name }, { $inc: { 'conditions.$.curStack': 1 }, $set: { 'conditions.$.curDuration': maxDuration } })
else
Souls.update({ _id: target._id, 'conditions.name': name }, { $set: { 'conditions.$.curDuration': maxDuration } })
else if existingCondition and !stackable
Souls.update({ _id: target._id, 'conditions.name': name }, { $set: { 'conditions.$.curDuration': maxDuration } })
else
effect = _.extend(effect, {curDuration: curDuration, curStack: curStack})
Souls.update(_id: target._id, {$addToSet: { conditions: effect }})
#
#将新条件实例添加到目标
#
附加条件:(效果,目标)->
name=effect.name
maxDuration=effect.maxDuration
curDuration=如果为effect.curDuration,则为effect.curDuration,否则为maxDuration
maxStack=effect.maxStack
curStack=如果为effect.curStack,则为effect.curStack,否则为1
stackable=如果为maxStack,则为true,否则为false
如果target.location只需在您的文档中添加一个类型
(/类
/集合
)字段:
Character.prototype.type = ->
Character
Soul.prototype.type = ->
Soul
...
target.type.update ...
不知何故,prototype对象不可用,它抛出TypeError:无法设置UndefinedCharacter的属性'type'?如何定义Character?@Characters=new Meteor.Collection('Characters')在伪代码中,您有target.is(Character)
,因此我假设您还定义了一个单数字符。如果您没有这样做,那么通过定义Character=function(doc){{uu.extend(this,doc);}代码>和转换集合:Characters=newmongo.collection('Characters',{transform:function(doc){returnnewcharacter(doc);}})代码>