皮划艇像URL而不是查询参数

时间:2015-06-28 09:02:14

标签: ember.js

我有一个小应用程序,它从URL中读取参数。

示例:http://localhost:4200/flights?from=FRA&to=JFK

我想为用户提供类似以下网址的皮卡: http://localhost:4200/flights/FRA-JFK

可以在没有在网络服务器中进行映射的情况下使用Ember吗?

应用/控制器/ flights.js

import Ember from 'ember';

export default Ember.Controller.extend({
  queryParams: ['from','to'],
  from: null,
  to: null,

  filteredFromFlights: function() {
    var from = this.get('from');
    var flights = this.get('model');

    if (from) {
      return flights.filterBy('from', from);
    } else {
      return flights;
    }
  }.property('from', 'to', 'model'),

  filteredFlights: function() {
    var to = this.get('to');
    var flights = this.get('filteredFromFlights');

    if (to) {
      return flights.filterBy('to', to);
    } else {
      return flights;
    }
  }.property('from', 'to', 'model')
});

1 个答案:

答案 0 :(得分:1)

您可以添加嵌套路线(例如from-to),因此flights路由负责查找flightsfrom-to路由负责本地过滤flights通过param。

//router
this.route('flights', function() {
   this.route('from-to', {path: '/:from_to'});
});

//flights route
model: function() {
  return this.store.find('flight');
}

//from-to route
model: function(params) {
  var splited = params.from_to.split('-');
  return {
    from: splited[0],
    to: splited[1],
    model: this.modelFor('flights'); 
  };
},
setupController: function(controller, models) {
   controller.setProperties(models);
}

//from-to controller
filteredFlights: function() { 
  //...
}.property('from', 'to', 'model.[]'),

filteredFromFlights: function() { 
  //...
}.property('from', 'to', 'model.[]'),