Refactor a way we setup things on repo controller

Till now, when switching between different views, we were switching
different bindings on repo controller. This was quite innefficient,
because then we needed to add bindings also from other controllers and
it's hard to manage such structure when we would like to add specialized
controllers (like LogController).

The new setup is more declarative, meaning that we do such things on the
router and set things on proper controllers. The only drawback is that
now we need to setup a few observers instead of bindings for "current"
views (ie. when viewing the newest repo or when viewing the last build
in current repo).

At this point it may not look like huge improvement, but it will open a
way to more refactorings.
This commit is contained in:
Piotr Sarnacki 2013-06-05 11:15:51 +02:00
parent f6fde27258
commit 6bc7b69088
4 changed files with 43 additions and 25 deletions

View File

@ -1,7 +1,6 @@
Travis.BuildController = Ember.Controller.extend
needs: ['repo']
repoBinding: 'controllers.repo.repo'
buildBinding: 'controllers.repo.build'
commitBinding: 'build.commit'
lineNumberBinding: 'controllers.repo.lineNumber'

View File

@ -5,7 +5,6 @@ Travis.BuildsController = Em.ArrayController.extend
needs: ['repo']
repoBinding: 'controllers.repo.repo'
contentBinding: 'controllers.repo.builds'
tabBinding: 'controllers.repo.tab'
isLoadedBinding: 'content.isLoaded'

View File

@ -22,39 +22,27 @@ Travis.RepoController = Travis.Controller.extend
jobs.forEach (j) -> j.updateTimes()
activate: (action) ->
@_unbind()
this["view#{$.camelize(action)}"]()
viewIndex: ->
@_bind('repo', 'controllers.repos.firstObject')
@_bind('build', 'repo.lastBuild')
@connectTab('current')
viewCurrent: ->
@connectTab('current')
@_bind('build', 'repo.lastBuild')
viewBuilds: ->
@connectTab('builds')
@_bind('builds', 'repo.builds')
viewPullRequests: ->
@connectTab('pull_requests')
@_bind('builds', 'repo.pullRequests')
viewBranches: ->
@connectTab('branches')
@_bind('builds', 'repo.branches')
viewEvents: ->
@connectTab('events')
@_bind('events', 'repo.events')
viewBuild: ->
@connectTab('build')
viewJob: ->
@_bind('build', 'job.build')
@connectTab('job')
connectTab: (tab) ->
@ -68,13 +56,6 @@ Travis.RepoController = Travis.Controller.extend
@set('tab', tab)
_bind: (to, from) ->
@bindings.push Ember.oneWay(this, to, from)
_unbind: ->
binding.disconnect(this) for binding in @bindings
@bindings.clear()
urlGithub: (->
Travis.Urls.githubRepo(@get('repo.slug'))
).property('repo.slug')

View File

@ -116,20 +116,56 @@ Travis.ApplicationRoute = Ember.Route.extend Travis.LineNumberParser,
this.controllerFor('repo').set('lineNumber', @fetchLineNumber())
Travis.IndexCurrentRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin,
Travis.SetupLastBuild = Ember.Mixin.create
setupController: ->
@lastBuildDidChange()
@controllerFor('repo').addObserver('repo.lastBuild', this, 'lastBuildDidChange')
deactivate: ->
@_super.apply this, arguments
@controllerFor('repo').removeObserver('repo.lastBuild', this, 'lastBuildDidChange')
lastBuildDidChange: ->
build = @controllerFor('repo').get('repo.lastBuild')
@controllerFor('build').set('build', build)
Travis.IndexCurrentRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin, Travis.SetupLastBuild,
renderTemplate: ->
@render 'repo'
@render 'build', outlet: 'pane', into: 'repo'
setupController: ->
@_super.apply this, arguments
@currentRepoDidChange()
@container.lookup('controller:repo').activate('index')
@controllerFor('repos').addObserver('firstObject', this, 'currentRepoDidChange')
deactivate: ->
@controllerFor('repos').removeObserver('firstObject', this, 'currentRepoDidChange')
currentRepoDidChange: ->
@controllerFor('repo').set('repo', @controllerFor('repos').get('firstObject'))
Travis.AbstractBuildsRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin,
renderTemplate: ->
@render 'builds', outlet: 'pane', into: 'repo'
setupController: ->
@container.lookup('controller:repo').activate(@get('contentType'))
@controllerFor('repo').activate(@get('contentType'))
@contentDidChange()
@controllerFor('repo').addObserver(@get('path'), this, 'contentDidChange')
deactivate: ->
@controllerFor('repo').removeObserver(@get('path'), this, 'contentDidChange')
contentDidChange: ->
path = @get('path')
@controllerFor('builds').set('content', @controllerFor('repo').get(path))
path: (->
type = @get('contentType')
"repo.#{type.camelize()}"
).property('contentType')
Travis.BuildsRoute = Travis.AbstractBuildsRoute.extend(contentType: 'builds')
Travis.PullRequestsRoute = Travis.AbstractBuildsRoute.extend(contentType: 'pull_requests')
@ -163,12 +199,15 @@ Travis.JobRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin,
setupController: (controller, model) ->
model = Travis.Job.find(model) if model && !model.get
repo = @container.lookup('controller:repo')
repo = @controllerFor('repo')
repo.set('job', model)
repo.activate('job')
@controllerFor('build').set('build', model.get('build'))
repo.set('build', model.get('build'))
Travis.RepoIndexRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin,
Travis.RepoIndexRoute = Ember.Route.extend Travis.DontSetupModelForControllerMixin, Travis.SetupLastBuild,
setupController: (controller, model) ->
@_super.apply this, arguments
@container.lookup('controller:repo').activate('current')
renderTemplate: ->