关于控制器在Ember中的典型行为 [英] Concerning the Typical Behavior of Controllers in Ember

查看:195
本文介绍了关于控制器在Ember中的典型行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ember.js中的控制器是否意图绑定到主视图区域/场景,或者更多地绑定到一组数据?



在ember中有几个主要视图与同一个控制器绑定是否常见?



在iOS中屏幕的主要部分或部分连接到单个控制器。如果你想提供另一个主界面,说一个模态窗口来创建一个新的元素,你(通常)有一个完全独立的控制器来管理这个视图及其数据/逻辑。



在像Zend Framework这样的东西中,你有一些控制器可能会执行一些常见的启动步骤来确保身份验证,但是在很大程度上这些操作会发挥控制器在iOS中的作用,处理逻辑并提供1个主要部分或视图的数据(作为网络,这通常是整个页面)。



在ember中使用控制器的典型角色或建议模式是什么?

解决方案

你在这里有几个不同的问题,所以我一次就解决一个问题。



首先,您询问控制器是面向数据还是面向视图。根据我的经验,两种行为都是允许的控制器是管理应用程序数据集的好方法,包括过滤和搜索等。 Evin Grano从SproutCore的角度写了一篇关于这个的好帖子,大部分概念也适用于Ember: http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/ 。控制器也非常适合控制应用程序的状态和行为。例如,您可能会将控制器中的方法放在应用程序其他位置的按钮操作中。但是,您还应该检查Ember States,看看这些可能更适合您的方案。



其次,您询问将多个视图绑定到同一个控制器。就个人而言,只要控制器保持不同的目的,我就不会看到这个问题。如果视图在逻辑上相关并且共享在相同的状态或数据中,则单个控制器是有意义的。如果您发现控制器的增长覆盖太多不同的区域,那么您应该考虑将其分为多个控制器。


Are controllers in ember.js meant to be tied to main view areas/scenes a la iOS, or more tied to a set of data?

Is it common/wise to have several main views tied to the same controller in ember?

In iOS main portions or sections of the screen are tied to a single controller. If you want to present another main interface, say a modal window to create a new element, you (typically) have an entirely separate controller to manage that view and its data/logic.

In something like Zend Framework, you have controllers that might perform some common spinup steps of ensuring authentication, but largely the actions play the role that controllers do in iOS, handling the logic and providing the data for 1 main section or view (being the web, this usually ends up being the whole page).

What is that typical role or advised pattern for using controllers in ember?

解决方案

You have a couple different questions here so I'll address them one at a time.

First, you asked if controllers should be data oriented or view oriented. In my experience both behaviors are allowable. Controllers are an excellent way to manage data sets for your application, including things like filtering and searching. Evin Grano wrote a good post about this from the SproutCore perspective and most of the concepts should apply to Ember as well: http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/. Controllers are also well suited for controlling the application state and behavior. For example, you might put a method in a controller that is bound to as a button action elsewhere in your app. However, you should also examine Ember States to see if these might be better suited to your scenario.

Secondly, you asked about tying multiple views to the same controller. Personally, I see no concerns with this as long as the controller maintains a distinct purpose. If the views are logically related and share in the same state or data then a single controller makes sense. If you find the controller growing to cover too many different areas, you should consider splitting it into multiple controllers.

这篇关于关于控制器在Ember中的典型行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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