2

成功保存新的 Project 对象后,我应该如何处理转换到不同的视图?如果我的验证通过,我想提交 @transaction 并转换到 Projects 索引视图。

控制器:

DA.ProjectsController = Em.ArrayController.extend
  createProject: (project) ->
    @transaction = DA.store.transaction()
    p = @transaction.createRecord(DA.Project, project)
    validation_errors = p.validate()

    if validation_errors
      DA.displayError validation_errors
    else
      @transaction.commit()

      ### What should be here to transition the route/view? ###
      router.transitionTo('projects') # Like this?
      DA.get('router').send('projects') # This?
      ###################################################

路由器:

DA.Router = Em.Router.create
  location: 'history'

  root: Em.Route.extend
    index: Em.Route.extend
      route: '/'
      redirectsTo: 'projects'

    projects: Em.Route.extend
      route: '/projects'
      index: Em.Route.extend
        router: '/'
      connectOutlets: (router) ->
        router.get('applicationController').connectOutlet('projects', DA.Project.find())
      showProject: Em.Route.transitionTo('project')

    project: Em.Route.extend
      route: '/projects/:project_id'
      connectOutlets: (router, project) ->
        router.get('applicationController').connectOutlet('project', project)

    newProject: Em.Route.extend
      route: '/projects/new'
      connectOutlets: (router) ->
        router.get('applicationController').connectOutlet('projectsNew')

    doProjects: (router) ->
      router.transitionTo('projects')
    doProjectsNew: (router) ->
      router.transitionTo('newProject')
4

1 回答 1

1

我想我可能已经解决了自己的问题,但如果有更合适的方法,我很想听听其他人的意见。这就是我最终的结果:

DA.ProjectsController = Em.ArrayController.extend    
  createProject: (project) ->
    @transaction = DA.store.transaction()
    p = @transaction.createRecord(DA.Project, project)
    validation_errors = p.validate()

    if validation_errors
      DA.displayError validation_errors
    else
      @transaction.commit()
      DA.get('router').transitionTo('projects')
于 2012-08-22T19:50:12.963 回答