1

在路由器 V1 中,您可以创建一个简单的 goBack 功能,如下所示:

App.Router = Ember.Router.extend({
  root: Ember.Route.extend({
    index: Ember.Route.extend({
        route: '/',
        redirectsTo: 'posts'
    }),
    posts: Ember.Route.extend({
        route: '/posts',
        showPost: Ember.Route.transitionTo('post'),
        connectOutlets: function(router){
           router.get('applicationController').
               connectOutlet('posts',App.Post.find());
        }
    }),
    post: Ember.Route.extend({
        route: '/posts/:post_id',
        goBack: Ember.Route.transitionTo('posts'),
        connectOutlets: function(router, post) {
            router.get('applicationController').connectOutlet('post', post);
        }
    })
  })
});

我正在尝试在路由器 v2 中做同样的事情,并提出了以下解决方案:

App.ApplicationController = Ember.Controller.extend({
  currentPathDidChange: function () {
    this.set('_previousPath', this.get('_currentPath'));
    this.set('_currentPath', this.get('currentPath'));
  }.observes('currentPath')
});

App.GobackRoute = Ember.Route.extend({
  redirect: function (model) {
    var previousPath = this.controllerFor('application').get('_previousPath');

    var parts = previousPath.split(".");

    var router = this.get('router');
    if (router.hasRoute(parts[parts.length - 1])) {
        this.transitionTo(parts[parts.length - 1]);
    } else if (router.hasRoute(parts[parts.length - 2] + "." + parts[parts.length - 1])) {
        this.transitionTo(parts[parts.length - 2] + "." + parts[parts.length - 1]);
    } else {
        Ember.Logger.warn('No route for: %s', previousPath);
    }
  }
});

没有更简单的解决方案吗?

jsFiddle

我想重用路由、控制器等......而不会导致某种意大利面条式路由,比如

App.AnimalsRoute = Ember.Route.extend({
  events: {
    goBackToThis: function() {
        this.transitionTo('this');
    },
    goBackToThat: function() {
        this.transitionTo('that');
    },
    goBackToSomeThingElse: function() {
        this.transitionTo('someThingElse');
    }
  } 
});

我想为整个路由器提供 1 个回退功能!我最初的解决方案结果如下:(查找所有 goBack 路线,以及 ebed 路线的重用)

App.Router.map(function (match) {
    this.route('home', { path: '/' });
    this.route('logout');
    this.route('login');
    this.resource('goBack', { path: '/goback' });

    this.resource('ccpr', function () {
        this.resource('goBack', { path: '/goback' });
        this.resource('ccprPatients',  { path: '/' }, function () {
            this.route('search');
        });
        this.resource('ccprPatient', { path: '/:ccpr_patient_id' }, function () {
            this.resource('goBack', { path: '/goback' });       
            this.resource('ccprPracticeSessions', { path: '/practicesessions' }, function () {
            });
            this.resource('ccprPracticeSession', { path: '/practicesessions/:ccpr_practicesession_id' }, function () {
                this.route('info');
                this.route('anamnese');
                this.route('medication');
                this.route('trainingModel', { path: '/trainingmodel' });
                this.route('socialEvaluation', { path: '/socialevaluation' });
                this.route('medicalFollowUp', { path: '/medicalfollowup' });
                this.route('psychologicalEvaluation', { path: '/psychologicalevaluation' });
                this.route('nutritionalAdvice', { path: '/nutritionaladvice' });

                this.resource('goBack', { path: '/goback' });

                this.resource('ebedMedication', { path: '/ebedmedication/:ebed_medication_id' }, function () {
                });
                this.resource('ebedLabResult', { path: '/ebedlabresult/:ebed_labresult_id' }, function () {
                });
                this.resource('ebedDietContact', { path: '/ebeddietcontact/:ebed_dietcontact_id' }, function () {
                });
                this.resource('ebedNutritionBmi', { path: '/ebednutritionbmi/:ebed_nutritionbmi_pkid' }, function () {
                });
            });
        });
        this.resource('ccprCardioArticles', { path: "/cardioarticles" });
        this.resource('ccprCardiologists', { path: "/cardiologists" });
        this.resource('ccprInfoSession', { path: "/infosession" });
        this.resource('ccprPatientPresence', { path: "/patientpresence" });
        this.resource('ccprPresenceOverview', { path: "/presenceoverview" });
        this.resource('ccprNextNutritionalAdvices', { path: "/nextnutritionaladvices" });
    });
    this.resource('ebed', function () {
        this.resource('goBack', { path: '/goback' });
        this.resource('ebedMedications', { path: '/ebedmedications' }, function () {
        });
        this.resource('ebedMedication', { path: '/ebedmedication/:ebed_medication_id' }, function () {
        });
        this.resource('ebedLabResults', { path: '/ebedlabresults' }, function () {
        }); 
        this.resource('ebedLabResult', { path: '/ebedlabresult/:ebed_labresult_id' }, function () {
        });
        this.resource('ebedDietContacts', { path: '/ebeddietcontacts' }, function () {
        });     
        this.resource('ebedDietContact', { path: '/ebeddietcontact/:ebed_dietcontact_id' }, function () {
        });
        this.resource('ebedNutritionBmis', { path: '/ebednutritionbmis' }, function () {
        });         
        this.resource('ebedNutritionBmi', { path: '/ebednutritionbmi/:ebed_nutritionbmi_pkid' }, function () {
        }); 
    });
});

没有更好的方法吗?

4

1 回答 1

1

更新了 JSFiddle 示例

events您可以使用路由器中的属性在新路由器中执行相同的操作来定义操作。从指南

如果在当前控制器上找不到该操作,它将冒泡到当前路由处理程序。从那里,它将冒泡到父路由处理程序,直到它到达应用程序路由。

在您的示例中,您需要在和路由goBack的父级中定义操作;animals.doganimals.cat

App.AnimalsRoute = Ember.Route.extend({
    events: {
        goBack: function() {
            this.transitionTo('index');
        }
    } });

这可以使用{{action}}模板中的标准助手调用。

<a href='#' {{action goBack}}>Go Back</a>
于 2013-01-29T15:44:25.680 回答