如何处理Ember.js控制器的路由器转换?

时间:2012-08-22 19:01:15

标签: ember.js ember-data

成功保存新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')

1 个答案:

答案 0 :(得分: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')