如何在 ember 2.17 中使用带有多个参数的嵌套路由

panx36

我正在尝试构建一个智能家居应用程序,但我坚持使用多个参数调用嵌套路由。我想显示用户登录的信息以及在我父路由中的模板下方,我想呈现子页面以显示家庭。选择特定家庭后,我想显示家庭中的房间,然后显示设备。这是我的 router.js

    Router.map(function() {
      this.route('about');
      this.route('users', function() {
      });
      this.route('households', { path: '/:user_id' }, function() {
        this.route('index', { path: '/:user_id' })
        this.route('rooms',{ path: '/:household_id' });
        this.route('devices', { path: '/:room_id' });
      });
    });

export default Router;

我链接到这样的家庭

  <h3>{{#link-to "households" user.id}}{{user.surname}}{{/link-to}}</h3>

现在我想在houses.js 的路由中声明一个模型,它从ember 数据存储返回一个用户并呈现父模板。之后,模型也应该使用 user.id 重定向到houses.index,并且familys.index.hbs 应该呈现父模板下的所有家庭。我的houses.js 路由如下所示:

export default Route.extend({
  model(params){
    {
      return this.get('store').findRecord('user', params.user_id);
    }
  }
});

和我的 home.index 路线像这样

export default Route.extend({
    model(params) {
       return this.get('store').findAll('household').then(results => results.filter((site) => {
           return site.get('member').filter(x => x == params.user_id).length > 0;
       }));
      }
});

实际上会出现以下错误:

错误:断言失败:您试图定义一个{{link-to "households.rooms"}}但没有传递生成其动态段所需的参数。您必须user_idgenerate.

一般来说,我需要在所有嵌套路由/子路由中使用多个参数,因为我需要例如在路由设备中使用 user_id 来检查调用用户是否是管理员。如果他是管理员,他将能够添加和编辑设备。而且我需要 room_id 来仅显示所选房间中的设备。

有什么方法可以传递多个参数或以某种方式使用控制器,我可以处理我的目的吗?

铁匠BG

据我了解,您还没有很好地设置路由层次结构。

假设你有多个用户,每个用户有多个家庭,每个家庭有多个房间,我建议你router.js是这样的:

Router.map(function() {
  this.route('about');
  this.route('users', function() {
    this.route('index'); // Lists all the users, URL looks like /users
    this.route('single', { path: '/:user_id' }, function() {
      this.route('index'); // Shows a single user, URL looks like /users/123
      this.route('households', function() {
        this.route('index'); // Shows all households a user with user_id has, URL looks like /users/123/households
        this.route('single',{ path: '/:household_id' }, function() {
          this.route('index'); // Shows a single household, URL looks like /users/123/households/456
          this.route('rooms', function() {
            this.route('index'); // Shows all rooms a household with household_id has, URL looks like /users/123/households/456/rooms
            this.route('single', { path: '/:room_id' }); // Shows a single room, URL looks like /users/123/households/456/rooms/789
          });
        });
      });
    });
  });
});

this.route('index');如果您愿意,可以随意省略路由器中的线路,但请确保您制作了一条路线来处理此问题。您的模板应如下所示。

// templates/users.hbs

{{outlet}}


// templates/users/index.hbs

<h1>This shows all the users</h1>

<ul>
{{#each model as |user|}}
  <li>{{user.name}}</li>
{{/each}}
</ul>


// templates/users/single.hbs

{{outlet}}


// templates/users/single/index.hbs

<h1>This shows a single user with id {{model.id}}</h1>

<p>This is the user named {{model.name}}.</p>


// templates/users/single/households.hbs

{{outlet}}


// ... And so on

你应该model()以这样的方式实现钩子,它们只获取你真正需要的东西。对于列表,你获取它们在指数route.js的类型要显示(即model()routes/users/index.js用户),而对于单个记录在索引中,但在单route.js(即单个家庭在model()routes/users/single/households/single.js),以使索引路由和子路由都可以访问该模型。

因此,使用此配置,您的链接应如下所示:

// All users
{{#link-to 'users'}}All users{{/link-to}}

// Single user
{{#link-to 'users.single' user.id}}{{user.name}}{{/link-to}}

// Households of a single user
{{#link-to 'users.single.households' user.id}}All households of {{user.name}}{{/link-to}}

// Specific household of a single user
{{#link-to 'users.single.households.single' user.id household.id}}Household {{household.name}} of {{user.name}}{{/link-to}}

// Rooms within a specific household
{{#link-to 'users.single.households.single.rooms' user.id household.id}}All rooms within household {{household.name}} of {{user.name}}{{/link-to}}

注意:确保正确指定模型及其关系,以便从一开始就让您的生活更轻松。因此,对于本答案开头假设的配置,您应该使模型如下所示:

// models/user.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),

    households: DS.hasMany('household')
});


// models/household.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),

    user: DS.belongsTo('user'),
    rooms: DS.hasMany('room')
});


// models/room.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),

    household: DS.belongsTo('household')
});

如果你像这样组织你的模型,那么 Ember 会让你从房间页面(路由)链接到用户页面,如下所示:

{{#link-to 'users.single' model.household.user}}Go to user{{/link-to}}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章