Normalize clan owner data

This commit is contained in:
Matt Lott 2015-04-03 09:52:25 -07:00
parent 56342ad993
commit f0bee3fd98
6 changed files with 57 additions and 24 deletions

View file

@ -7,7 +7,6 @@ _.extend ClanSchema.properties,
name: c.shortString()
type: {type: 'string', 'enum': ['public']}
ownerID: c.objectId()
ownerName: c.shortString()
members: c.array {title: 'Members'}, c.objectId()
c.extendBasicProperties ClanSchema, 'Clan'

View file

@ -2,10 +2,12 @@ extends /templates/base
block content
h1= clan.get('name')
span.spr.spl.small (owner
a(href="/user/#{clan.get('ownerID')}")= clan.get('ownerName')
| )
if clan
h1= clan.get('name')
if owner
span.spr.spl.small (owner
a(href="/user/#{clan.get('ownerID')}")= owner.get('name')
| )
div
if isOwner
button.btn.btn-sm.btn-warning.delete-clan-btn Delete Clan
@ -14,8 +16,8 @@ block content
else
button.btn.btn-sm.btn-success.join-clan-btn Join Clan
if clan.get('members')
h3 Clan Members (#{clan.get('members').length})
if members
h3 Clan Members (#{members.length})
table.table.table-striped.table-condensed
thead
tr

View file

@ -35,7 +35,10 @@ block content
a(href="/clans/#{clan.id}")= clan.get('name')
td= clan.get('members').length
td
a(href="/user/#{clan.ownerID}")= clan.get('ownerName')
if idNameMap && idNameMap[clan.get('ownerID')]
a(href="/user/#{clan.get('ownerID')}")= idNameMap[clan.get('ownerID')]
else
a(href="/user/#{clan.get('ownerID')}") Anoner
td
if myClanIDs.indexOf(clan.id) < 0
button.btn.btn-sm.btn-success.join-clan-btn(data-id="#{clan.id}") Join Clan
@ -63,7 +66,10 @@ block content
a(href="/clans/#{clan.id}")= clan.get('name')
td= clan.get('members').length
td
a(href="/user/#{clan.ownerID}")= clan.get('ownerName')
if idNameMap && idNameMap[clan.get('ownerID')]
a(href="/user/#{clan.get('ownerID')}")= idNameMap[clan.get('ownerID')]
else
a(href="/user/#{clan.get('ownerID')}") Anoner
td
if clan.get('ownerID') !== me.id
button.btn.btn-sm.btn-warning.leave-clan-btn(data-id="#{clan.id}") Leave Clan

View file

@ -21,13 +21,7 @@ module.exports = class ClanDetailsView extends RootView
constructor: (options, @clanID) ->
super options
@clan = new Clan _id: @clanID
@listenTo @clan, 'sync', => @render?()
@supermodel.loadModel @clan, 'clan', cache: false
@members = new CocoCollection([], { url: "/db/clan/#{clanID}/members", model: User, comparator:'_id' })
@listenTo @members, 'sync', => @render?()
@supermodel.loadCollection(@members, 'members', {cache: false})
@listenTo me, 'sync', => @render?()
@initData()
destroy: ->
@stopListening?()
@ -35,11 +29,25 @@ module.exports = class ClanDetailsView extends RootView
getRenderData: ->
context = super()
context.clan = @clan
context.owner = @owner
context.members = @members.models
context.isOwner = @clan.get('ownerID') is me.id
context.isMember = @clanID in me.get('clans')
context
initData: ->
@clan = new Clan _id: @clanID
@listenTo @clan, 'sync', => @render?()
@listenToOnce @clan, 'sync', =>
@owner = new User _id: @clan.get('ownerID')
@listenTo @owner, 'sync', => @render?()
@supermodel.loadModel @owner, 'owner', cache: false
@supermodel.loadModel @clan, 'clan', cache: false
@members = new CocoCollection([], { url: "/db/clan/#{clanID}/members", model: User, comparator:'_id' })
@listenTo @members, 'sync', => @render?()
@supermodel.loadCollection(@members, 'members', {cache: false})
@listenTo me, 'sync', => @render?()
onDeleteClan: (e) ->
return @openModalView(new AuthModal()) if me.isAnonymous()
options =

View file

@ -20,24 +20,43 @@ module.exports = class MainAdminView extends RootView
constructor: (options) ->
super options
@publicClans = new CocoCollection([], { url: '/db/clan', model: Clan, comparator:'_id' })
@listenTo @publicClans, 'sync', => @render?()
@supermodel.loadCollection(@publicClans, 'public_clans', {cache: false})
@myClans = new CocoCollection([], { url: '/db/user/-/clans', model: Clan, comparator:'_id' })
@listenTo @myClans, 'sync', => @render?()
@supermodel.loadCollection(@myClans, 'my_clans', {cache: false})
@listenTo me, 'sync', => @render?()
@initData()
destroy: ->
@stopListening?()
getRenderData: ->
context = super()
context.idNameMap = @idNameMap
context.publicClans = @publicClans.models
context.myClans = @myClans.models
context.myClanIDs = me.get('clans') ? []
context
initData: ->
@idNameMap = {}
@publicClans = new CocoCollection([], { url: '/db/clan', model: Clan, comparator:'_id' })
@listenTo @publicClans, 'sync', =>
@refreshNames @publicClans.models
@render?()
@supermodel.loadCollection(@publicClans, 'public_clans', {cache: false})
@myClans = new CocoCollection([], { url: '/db/user/-/clans', model: Clan, comparator:'_id' })
@listenTo @myClans, 'sync', =>
@refreshNames @myClans.models
@render?()
@supermodel.loadCollection(@myClans, 'my_clans', {cache: false})
@listenTo me, 'sync', => @render?()
refreshNames: (clans) ->
options =
url: '/db/user/-/names'
method: 'POST'
data: {ids: _.map(clans, (clan) -> clan.get('ownerID'))}
success: (models, response, options) =>
@idNameMap[userID] = models[userID].name for userID of models
@render?()
@supermodel.addRequestResource('user_names', options, 0).load()
onClickCreateClan: (e) ->
return @openModalView(new AuthModal()) if me.isAnonymous()
if name = $('.create-clan-name').val()

View file

@ -26,7 +26,6 @@ ClanHandler = class ClanHandler extends Handler
userName = req.user.get('name') ? 'Anoner'
instance = super(req)
instance.set 'ownerID', req.user._id
instance.set 'ownerName', userName
instance.set 'members', [req.user._id]
instance