First stab at fixing weird view errors

This commit starts refactoring of one of the remaining areas where we do weird
tricks to get the desired behaviour. Namely, we were treating "my_repositories"
and "recent" not as individual routes with separate URLs, but only different
states on the repos controller. Such approach leads to various problem with
connecting outlets on rerenders (ie. we don't explicitly connect outlets when
changing from one view to another programatically).

A new cleaner way is to change both tabs into routes.
This commit is contained in:
Piotr Sarnacki 2014-12-23 00:04:44 +01:00
parent 0dfb5dc10c
commit 5f2f4c3852
7 changed files with 53 additions and 50 deletions

View File

@ -3,23 +3,8 @@ require 'travis/limited_array'
Travis.ReposController = Ember.ArrayController.extend
actions:
activate: (name) ->
@transitionToRoot()
@activate(name)
defaultTab: ( ->
if @get('currentUser.id')
'owned'
else
'recent'
).property('currentUser.id')
currentUserIdDidChange: (->
if @get('currentUser.id')
@activate('owned')
else if @get('tab') == 'owned'
@activate('recent')
).observes('currentUser.id')
tabOrIsLoadedDidChange: (->
@possiblyRedirectToGettingStartedPage()
).observes('isLoaded', 'tab', 'length')
@ -58,13 +43,8 @@ Travis.ReposController = Ember.ArrayController.extend
if content = @get('content')
content.forEach (r) -> r.updateTimes()
transitionToRoot: ->
@container.lookup('router:main').send('renderDefaultTemplate')
@container.lookup('router:main').transitionTo('index.current')
activate: (tab, params) ->
@set('sortProperties', ['sortOrder'])
tab ||= @get('defaultTab')
@set('tab', tab)
this["view#{$.camelize(tab)}"](params)

View File

@ -43,14 +43,17 @@ Travis.ApplicationRoute = Travis.Route.extend
if transition = @auth.get('afterSignInTransition')
@auth.set('afterSignInTransition', null)
transition.retry()
else
@transitionTo('index')
afterSignOut: ->
@transitionTo('index.current')
@transitionTo('index')
Travis.Router.map ->
@resource 'index', path: '/', ->
@resource 'getting_started'
@route 'current', path: '/'
@route 'recent', path: '/recent'
@route 'my_repositories', path: '/my_repositories'
@resource 'repo', path: '/:owner/:name', ->
@route 'index', path: '/'
@resource 'build', path: '/builds/:build_id'
@ -130,7 +133,8 @@ Travis.GettingStartedRoute = Travis.Route.extend
Travis.SimpleLayoutRoute = Travis.Route.extend
setupController: ->
$('body').attr('id', 'home')
@container.lookup('controller:repos').activate()
toActivate = if @signedIn() then 'owned' else 'recent'
@container.lookup('controller:repos').activate(toActivate)
@_super.apply(this, arguments)
renderTemplate: ->
@ -148,7 +152,7 @@ Travis.InsufficientOauthPermissionsRoute = Travis.SimpleLayoutRoute.extend
existingUser = document.location.hash.match(/#existing[_-]user/)
controller.set('existingUser', existingUser)
Travis.IndexCurrentRoute = Travis.Route.extend
Travis.IndexMyRepositoriesRoute = Travis.Route.extend
renderTemplate: ->
@render 'repo'
@render 'build', into: 'repo'
@ -158,6 +162,34 @@ Travis.IndexCurrentRoute = Travis.Route.extend
@currentRepoDidChange()
@controllerFor('repo').activate('index')
@controllerFor('repos').activate('owned')
@controllerFor('repos').addObserver('firstObject', this, 'currentRepoDidChange')
afterModel: ->
@controllerFor('repos').possiblyRedirectToGettingStartedPage()
deactivate: ->
@controllerFor('repos').removeObserver('firstObject', this, 'currentRepoDidChange')
currentRepoDidChange: ->
@controllerFor('repo').set('repo', @controllerFor('repos').get('firstObject'))
actions:
redirectToGettingStarted: ->
@transitionTo('getting_started')
Travis.IndexRecentRoute = Travis.Route.extend
renderTemplate: ->
@render 'repo'
@render 'build', into: 'repo'
setupController: ->
@_super.apply this, arguments
@currentRepoDidChange()
@controllerFor('repo').activate('index')
@controllerFor('repos').activate('recent')
@controllerFor('repos').addObserver('firstObject', this, 'currentRepoDidChange')
afterModel: ->
@ -293,6 +325,13 @@ Travis.RepoRoute = Travis.Route.extend
# bubble to the top
return true
# Obviously Index route should be renamed to something
# like "main" or "home"
Travis.IndexIndexRoute = Travis.Route.extend
redirect: ->
target = if @signedIn() then 'my_repositories' else 'recent'
@transitionTo("index.#{target}")
Travis.IndexRoute = Travis.Route.extend
renderTemplate: ->
$('body').attr('id', 'home')
@ -302,7 +341,9 @@ Travis.IndexRoute = Travis.Route.extend
@render 'repos', outlet: 'left', into: 'index'
setupController: (controller)->
@container.lookup('controller:repos').activate()
# TODO: this is redundant with my_repositories and recent routes
toActivate = if @signedIn() then 'owned' else 'recent'
@container.lookup('controller:repos').activate(toActivate)
Travis.StatsRoute = Travis.Route.extend
renderTemplate: ->
@ -377,7 +418,7 @@ Travis.AuthRoute = Travis.Route.extend
actions:
afterSignIn: ->
@transitionTo('index.current')
@transitionTo('index')
return true
Travis.SettingsRoute = Travis.Route.extend

View File

@ -1,4 +1,4 @@
{{#link-to "index.current"}}
{{#link-to "index"}}
<div id="home">
<h1>Travis</h1>
</div>
@ -6,7 +6,7 @@
<ul id="navigation">
<li class="home">
{{#link-to "index.current"}}Home{{/link-to}}
{{#link-to "index"}}Home{{/link-to}}
</li>
<li>
<a href="http://blog.travis-ci.com">Blog</a>

View File

@ -1,10 +1,10 @@
<ul class="tabs">
<li id="tab_owned" {{bind-attr class="view.classOwned"}}>
<h5><a {{action "activate" "owned"}}>My Repositories</a></h5>
<h5>{{#link-to "index.my_repositories"}}My Repositories{{/link-to}}</h5>
</li>
<li id="tab_recent" {{bind-attr class="view.classRecent"}}>
<h5><a {{action "activate" "recent"}}>Recent</a></h5>
<h5>{{#link-to "index.recent"}}Recent{{/link-to}}</h5>
</li>
<li id="tab_search" {{bind-attr class="view.classSearch"}}>

View File

@ -47,7 +47,7 @@ Travis.FirstSyncView = Travis.View.extend
Ember.run.later this, ->
Travis.Repo.fetch(member: @get('controller.user.login')).then( (repos) ->
if repos.get('length')
self.get('controller').transitionToRoute('index.current')
self.get('controller').transitionToRoute('index')
else
self.get('controller').transitionToRoute('profile')
).then(null, (e) ->

View File

@ -2,7 +2,7 @@ require 'log'
require 'travis/lines_selector'
require 'travis/log_folder'
Log.DEBUG = true
Log.DEBUG = false
Log.LIMIT = 10000
Travis.reopen

View File

@ -14,24 +14,6 @@ Travis.reopen
@get('repos.isLoaded') && @get('repos.length') == 0
).property('repos.isLoaded', 'repos.length')
repoIsLoadedDidChange: (->
# Ember does not automatically rerender outlets and sometimes 'pane' outlet
# in repos/show.hbs is empty when view is rerendered without routing
# taking place. Try to render the default outlet in such case
# TODO: look into fixing it in more general way
Ember.run.schedule('afterRender', this, ->
pane = Ember.get('_outlets.pane')
if @get('controller.repo.isLoaded') && @_state == 'inDOM' &&
@get('controller.repo.lastBuild') &&
@get('controller.tab') == 'current' && (!pane || pane._state == 'destroyed')
view = @get('controller.container').lookup('view:build')
view.set('controller', @get('controller.container').lookup('controller:build'))
Ember.run.next =>
@set('_outlets', {}) if !@get('_outlets') && !@isDestroyed
@connectOutlet('pane', view) unless @isDestroyed
)
).observes('controller.repo.isLoaded')
statusImages: ->
@popupCloseAll()
view = Travis.StatusImagesView.create(toolsView: this)