我希望根据当前路线将多个变量切换为 true 或 false [英] I want multiple variables toggled as true or false depending on the current route

查看:11
本文介绍了我希望根据当前路线将多个变量切换为 true 或 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望多个变量根据当前路由切换为真或假,控制器在页面加载时检查.

I want multiple variables toggled as true or false depending on the current route, which the controller checks when the page loads.

VpcYeoman.SuperTableController = Ember.ArrayController.extend({  
    routedToLocations: false,
    routedToUsers: false,
    currentPath: '/',
    checkCurrentPath: function() {
      if ( currentPath == '/users')
        this.set( 'routedToUsers', true )
    } elsif ( currentPath == '/locations' ) {
        this.set( 'routedToLocations', true )
    }
});

superTable.hbs

superTable.hbs

{{#if routedToUsers}}
Users!
{{/if}}

{{#if routedToLocations}}
Locations
{{/if}}

在 users.hbs 中

in users.hbs

{{render superTable model}}//这将产生字符串'Users!'

在locations.hbs

in locations.hbs

{{render superTable model}}//这将产生字符串'Locations!'

或者我可以添加变量,比如 routedToUsers,在带有设定值的用户"控制器中.看起来像,

Or maybe i could add the variables, like routedToUsers, inside of the 'users' controller with set values. It would look like,

- users_controller.js -
    routedToUsers: true,
    canBeEdited: false,
    canBeDeleted: true,

这样,每个超级表都有那些变量,除非它们已经预定义.另一个例子.

This way, each super-table has those variables except they are already predefined. Another example.

 - locations_controller.js - 
       routedToUsers: false, // this is obviously false but for example's sake
       routedToLocations: true,
       canBeEdited: false,
       canBeDeleted: false,

因此,如果我单击另一个页面上的 #link-to 将我路由到用户",例如,控制器将使用checkCurrentPath"来确保我确实在用户页面上.

So if I clicked a #link-to on another page that routed me to 'users' for instance, the controller would use 'checkCurrentPath' to make sure that I was indeed on the users page.

推荐答案

在您的应用中实际上有两组主要的路径更改或transitions".

There are really two main groups of path changes or "transitions" that occur in your app.

首先: Ember 将初始化,路由器将用户转换到所需的上下文.如果您有嵌套资源,这可能需要多次转换.如果用户返回嵌套资源或重新加载页面,Ember 是智能的并且会重新加载上下文.因此,自动转换.

First: Ember will initialize and the router will transition the user into the desired context. This can take several transitions if you have nested resources. If a user returns to a nested resource or reloads the page, Ember is smart and will reload with context. Thus, the auto-transitioning.

第二:用户可能会根据您制作的任何 UI 在应用中进行转换.

Second: The user may transition through the app based on whatever UI you cooked up.

要解决两个问题中的第一个,您可能需要使用 setupController 挂钩初始化每个路由的控制器以激活 currentPath 属性.这样在页面加载时,路由将执行第一个 setter,然后观察者将处理任何基于用户的路径更改.

To address the first of two issues, you will probably need to initialize each route's controller to activate the currentPath property, using the setupController hook. That way on page load the route will do the first setter, then the observer will deal with any user-based path changes.

这是一个通用路径观察器,您可以将其放置在应用程序控制器中.它将解决两个问题中的第二个:

Here is a generic path observer that you can place in your application controller. It will address the second of the two issues:

App.ApplicationController = Ember.Controller.extend({
  needs: ['super-table', 'users', 'locations'],
  currentPathDidChange: function () {
    var path_base = this.get('currentPath').split('.')[0];
    this.set('controllers.super-table.currentPath', path_base); // give your super table template the path
    this.nullifyActiveFlags(); // You can write this yourself...
    switch(path_base){ // just give me the base resource
      case 'users':
        this.set('controllers.users.activePath', true);
        break;
      case 'locations':
        this.set('controllers.locations.activePath', true);
        break;
    }
  }.observes('currentPath'),
//...

我可能会将回调逻辑从观察者中提取出来并放入它自己的方法中,但这只是显示可能的伪代码.

I would probably pull that callback logic out of the observer and into its own method, but this is just pseudo-code to show what is possible.

您还应该将超级表 currentPath setter 重构为来自超级表控制器内的计算别名.

You should also refactor the super table currentPath setter to be a computed alias from within the super table controller.

如果需要任何额外的解释,请告诉我!我已经多次使用类似的模式,如果配置正确,它的效果非常好.

Let me know if there is any additional explanation needed! I've used similar patterns quite a few times and it works very nicely when configured correctly.

这篇关于我希望根据当前路线将多个变量切换为 true 或 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆