Refactor handling accounts
This commit changes a way we load accounts for profile view: * instead of using several views with profile controllers, always use one view to render hooks. This is achieved by redirecting to individual account page from main profile page (for example when going into /profile as a user drogus, the effective address will be /profile/drogus) * instead of using observers to wait for accounts to load I just use promise in ProfileRoute#model which effectively ensures that accounts are loaded at the time we want to select an individual account * profile controller is split into profile and account controller
This commit is contained in:
parent
459089f0cd
commit
52dea338ee
|
@ -62,6 +62,7 @@ Travis.RepoSettingsController = Em.ObjectController.extend
|
|||
|
||||
require 'controllers/accounts'
|
||||
require 'controllers/auth'
|
||||
require 'controllers/account'
|
||||
require 'controllers/build'
|
||||
require 'controllers/builds'
|
||||
require 'controllers/flash'
|
||||
|
|
35
assets/scripts/app/controllers/account.coffee
Normal file
35
assets/scripts/app/controllers/account.coffee
Normal file
|
@ -0,0 +1,35 @@
|
|||
Travis.AccountController = Ember.ObjectController.extend
|
||||
allHooks: []
|
||||
|
||||
init: ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
self = this
|
||||
Travis.on("user:synced", (->
|
||||
self.reloadHooks()
|
||||
))
|
||||
|
||||
toggle: (hook) ->
|
||||
hook.toggle()
|
||||
|
||||
reloadHooks: ->
|
||||
if login = @get('login')
|
||||
@set('allHooks', Travis.Hook.find(all: true, owner_name: login))
|
||||
|
||||
hooks: (->
|
||||
@reloadHooks() unless hooks = @get('allHooks')
|
||||
@get('allHooks').filter (hook) -> hook.get('admin')
|
||||
).property('allHooks.length', 'allHooks')
|
||||
|
||||
hooksWithoutAdmin: (->
|
||||
@reloadHooks() unless hooks = @get('allHooks')
|
||||
@get('allHooks').filter (hook) -> !hook.get('admin')
|
||||
).property('allHooks.length', 'allHooks')
|
||||
|
||||
showPrivateReposHint: (->
|
||||
Travis.config.show_repos_hint == 'private'
|
||||
) .property()
|
||||
|
||||
showPublicReposHint: (->
|
||||
Travis.config.show_repos_hint == 'public'
|
||||
) .property()
|
|
@ -1,20 +0,0 @@
|
|||
Travis.AccountIndexController = Em.Controller.extend
|
||||
needs: ['profile', 'currentUser']
|
||||
hooksBinding: 'controllers.profile.hooks'
|
||||
allHooksBinding: 'controllers.profile.allHooks'
|
||||
hooksWithoutAdminBinding: 'controllers.profile.hooksWithoutAdmin'
|
||||
userBinding: 'controllers.currentUser'
|
||||
|
||||
sync: ->
|
||||
@get('user').sync()
|
||||
|
||||
toggle: (hook) ->
|
||||
hook.toggle()
|
||||
|
||||
showPrivateReposHint: (->
|
||||
Travis.config.show_repos_hint == 'private'
|
||||
) .property()
|
||||
|
||||
showPublicReposHint: (->
|
||||
Travis.config.show_repos_hint == 'public'
|
||||
) .property()
|
|
@ -1,5 +1,2 @@
|
|||
Travis.AccountsController = Ember.ArrayController.extend
|
||||
tab: 'accounts'
|
||||
|
||||
findByLogin: (login) ->
|
||||
@find (account) -> account.get('login') == login
|
||||
|
|
|
@ -1,52 +1,19 @@
|
|||
Travis.ProfileController = Travis.Controller.extend
|
||||
name: 'profile'
|
||||
|
||||
needs: ['currentUser', 'accounts']
|
||||
needs: ['currentUser', 'accounts', 'account']
|
||||
userBinding: 'controllers.currentUser'
|
||||
accountsBinding: 'controllers.accounts'
|
||||
|
||||
init: ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
self = this
|
||||
Travis.on("user:synced", (->
|
||||
self.reloadHooks()
|
||||
))
|
||||
|
||||
account: (->
|
||||
login = @get('params.login') || @get('user.login')
|
||||
account = @get('accounts').filter((account) -> account if account.get('login') == login)[0]
|
||||
account.select() if account
|
||||
account
|
||||
).property('accounts.length', 'params.login')
|
||||
accountBinding: 'controllers.account'
|
||||
|
||||
sync: ->
|
||||
@get('user').sync()
|
||||
|
||||
toggle: (hook) ->
|
||||
hook.toggle()
|
||||
|
||||
activate: (action, params) ->
|
||||
@setParams(params || @get('params'))
|
||||
this["view#{$.camelize(action)}"]()
|
||||
|
||||
viewHooks: ->
|
||||
@connectTab('hooks')
|
||||
@reloadHooks()
|
||||
|
||||
reloadHooks: ->
|
||||
# TODO: figure out why user is not available sometimes
|
||||
@set('allHooks', Travis.Hook.find(all: true, owner_name: @get('params.login') || @get('user.login') || Travis.lookup('controller:currentUser').get('login')))
|
||||
|
||||
hooks: (->
|
||||
@reloadHooks() unless hooks = @get('allHooks')
|
||||
@get('allHooks').filter (hook) -> hook.get('admin')
|
||||
).property('allHooks.length', 'allHooks')
|
||||
|
||||
hooksWithoutAdmin: (->
|
||||
@reloadHooks() unless hooks = @get('allHooks')
|
||||
@get('allHooks').filter (hook) -> !hook.get('admin')
|
||||
).property('allHooks.length', 'allHooks')
|
||||
@get('controllers.account').reloadHooks()
|
||||
|
||||
viewUser: ->
|
||||
@connectTab('user')
|
||||
|
@ -54,15 +21,3 @@ Travis.ProfileController = Travis.Controller.extend
|
|||
connectTab: (tab) ->
|
||||
viewClass = Travis["#{$.camelize(tab)}View"]
|
||||
@set('tab', tab)
|
||||
|
||||
setParams: (params) ->
|
||||
@set('params', {})
|
||||
@set("params.#{key}", params[key]) for key, value of params
|
||||
|
||||
showPrivateReposHint: (->
|
||||
Travis.config.show_repos_hint == 'private'
|
||||
) .property()
|
||||
|
||||
showPublicReposHint: (->
|
||||
Travis.config.show_repos_hint == 'public'
|
||||
) .property()
|
||||
|
|
|
@ -72,7 +72,7 @@ Travis.Router.map ->
|
|||
@route 'auth', path: '/auth'
|
||||
|
||||
@resource 'profile', path: '/profile', ->
|
||||
@resource 'account', path: '/:login', ->
|
||||
@resource 'account', path: '/:login'
|
||||
@route 'info', path: '/info'
|
||||
|
||||
@route 'notFound', path: "/*path"
|
||||
|
@ -279,41 +279,36 @@ Travis.NotFoundRoute = Travis.Route.extend
|
|||
Travis.ProfileRoute = Travis.Route.extend
|
||||
needsAuth: true
|
||||
|
||||
setupController: ->
|
||||
setupController: (controller, model) ->
|
||||
@container.lookup('controller:application').connectLayout('profile')
|
||||
@container.lookup('controller:accounts').set('content', Travis.Account.find(all: true))
|
||||
@controllerFor('accounts').set('model', model)
|
||||
|
||||
model: ->
|
||||
Travis.Account.fetch(all: true)
|
||||
|
||||
renderTemplate: ->
|
||||
$('body').attr('id', 'profile')
|
||||
|
||||
@render 'accounts', outlet: 'left'
|
||||
|
||||
@_super.apply(this, arguments)
|
||||
|
||||
Travis.ProfileIndexRoute = Travis.Route.extend
|
||||
setupController: ->
|
||||
@container.lookup('controller:profile').activate 'hooks'
|
||||
|
||||
renderTemplate: ->
|
||||
@render 'hooks', controller: 'profile'
|
||||
redirect: ->
|
||||
# TODO: setting accounts model in ProfileRoute is wrong, but
|
||||
# at this stage it's better than what we had before
|
||||
accounts = @modelFor('profile')
|
||||
login = @controllerFor('currentUser').get('login')
|
||||
account = accounts.find (account) -> account.get('login') == login
|
||||
@transitionTo 'account', account
|
||||
|
||||
Travis.AccountRoute = Travis.Route.extend
|
||||
setupController: (controller, account) ->
|
||||
@_super.apply this, arguments
|
||||
|
||||
@controllerFor('profile').activate 'hooks'
|
||||
|
||||
model: (params) ->
|
||||
controller = @container.lookup('controller:accounts')
|
||||
account = controller.findByLogin(params.login)
|
||||
|
||||
if account
|
||||
account
|
||||
else
|
||||
content = Ember.Object.create(login: params.login)
|
||||
proxy = Ember.ObjectProxy.create(content: content)
|
||||
|
||||
observer = ->
|
||||
if account = controller.findByLogin(params.login)
|
||||
controller.removeObserver 'content.length', observer
|
||||
proxy.set('content', account)
|
||||
controller.addObserver 'content.length', observer
|
||||
|
||||
proxy
|
||||
@modelFor('profile').find (account) -> account.get('login') == params.login
|
||||
|
||||
serialize: (account) ->
|
||||
if account && account.get
|
||||
|
@ -321,18 +316,6 @@ Travis.AccountRoute = Travis.Route.extend
|
|||
else
|
||||
{}
|
||||
|
||||
Travis.AccountIndexRoute = Travis.Route.extend
|
||||
setupController: (controller) ->
|
||||
profileController = @container.lookup('controller:profile')
|
||||
profileController.activate 'hooks'
|
||||
|
||||
if account = @modelFor('account')
|
||||
params = { login: account.get('login') }
|
||||
profileController.setParams(params)
|
||||
|
||||
renderTemplate: ->
|
||||
@render 'hooks'
|
||||
|
||||
Travis.ProfileInfoRoute = Travis.Route.extend
|
||||
setupController: ->
|
||||
@container.lookup('controller:profile').activate 'user'
|
||||
|
|
1
assets/scripts/app/templates/loading.hbs
Normal file
1
assets/scripts/app/templates/loading.hbs
Normal file
|
@ -0,0 +1 @@
|
|||
<div class="loading"><span>Loading</span></div>
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<div class="tab">
|
||||
{{#collection Travis.AccountsListView contentBinding="controller"}}
|
||||
{{#link-to "account.index" view.account class="name"}}{{view.name}}{{/link-to}}
|
||||
{{#link-to "account" view.account class="name"}}{{view.name}}{{/link-to}}
|
||||
<p class="summary">
|
||||
<span class="repos_label">Repositories:</span>
|
||||
<abbr class="repos">{{view.account.reposCount}}</abbr>
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
<ul class="tabs">
|
||||
<li id="tab_hooks" {{bind-attr class="view.classHooks"}}>
|
||||
<h5>
|
||||
{{#with view.account}}
|
||||
{{#if login}}
|
||||
{{#link-to "account.index" this}}Repositories{{/link-to}}
|
||||
{{/if}}
|
||||
{{/with}}
|
||||
{{#link-to "account" account}}Repositories{{/link-to}}
|
||||
</h5>
|
||||
</li>
|
||||
{{#if view.displayUser}}
|
||||
|
|
|
@ -31,4 +31,4 @@
|
|||
).property('account.login')
|
||||
|
||||
click: ->
|
||||
@get('controller').transitionToRoute("account", @get('account.login'))
|
||||
@get('controller').transitionToRoute("account", @get('account.login'))
|
||||
|
|
|
@ -22,8 +22,6 @@ Travis.reopen
|
|||
'active' if @get('tab') == 'user'
|
||||
).property('tab')
|
||||
|
||||
accountBinding: 'controller.account'
|
||||
|
||||
displayUser: (->
|
||||
@get('controller.account.login') == @get('controller.user.login')
|
||||
).property('controller.account.login', 'controller.user.login')
|
||||
|
@ -42,4 +40,4 @@ Travis.reopen
|
|||
|
||||
gravatarUrl: (->
|
||||
"#{location.protocol}//www.gravatar.com/avatar/#{@get('user.gravatarId')}?s=200&d=mm"
|
||||
).property('user.gravatarId')
|
||||
).property('user.gravatarId')
|
||||
|
|
Loading…
Reference in New Issue
Block a user