发送的数据是Ruby格式:
mob = {id: 1, xpos:100, ypos:150, xposDest:150, yposDest:100, attacked:0}
WebsocketRails[:channel_name].trigger(:event_name, mob)
我怎么能让这个循环更简单? (这是coffeescript)
get_all_mobs: (mob) -> // getting "mob" with Websockets
mob_list[mob.id].id = mob.id
mob_list[mob.id].xpos = mob.xpos if mob.xpos?
mob_list[mob.id].ypos = mob.ypos if mob.ypos?
mob_list[mob.id].xposDest = mob.xposDest if mob.xposDest?
mob_list[mob.id].yposDest = mob.yposDest if mob.yposDest?
mob_list[mob.id].health = mob.health if mob.health?
mob_list[mob.id].level = mob.level if mob.level?
mob_list[mob.id].max_health = mob.max_health if mob.max_health?
mob_list[mob.id].attacked = mob.attacked if mob.attacked?
mob_list[mob.id].steps = mob.steps if mob.steps?
尝试了类似的东西,但错了:
get_all_mobs: (mob) -> // getting "mob" with Websockets
for attribute in mob
mob_list[mob.id].attribute = mob.attribute
答案 0 :(得分:4)
这应该做:
get_all_mobs: (mob) -> // getting "mob" with Websockets
for own key, value of mob
mob_list[mob.id][key] = value if value?