是否有任何理由不允许从所有部分视图访问所有数据?
是的。这个问题是在 Code Igniter 中出现的,但它与 Code Igniter 并没有真正的关系,因为每个 MVC php Web 应用程序都可能遇到这个问题。
将 MVC 视为应用程序设计,让我们想象一下视图
被分为部分视图
。默认情况下,部分视图
有自己的数据,就像视图
有自己的数据一样。现在我想知道:是否有任何理由不允许部分视图
访问视图
的数据?
Yep. This question came up looking at Code Igniter but it is not really related to code igniter due to the fact that every MVC php web app could hit the face on that.
Considering MVC as app design, let's imagine that a view
is divided into partial views
. By default partial views
has its own data as view
has its own. Now I was wondering: Is there any reason not to allow partial views
to have access to view
's data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
部分视图是为了简化在前端开发单个模块或组件的过程,不需要所有全局参数。
为了代码可用性和一点带宽,最好将发送的参数分开。
如果您确实希望传递所有参数,那么只需选择另一个视图模板而不是部分视图模板。
partial view is for simplicity of developing individual module or component in the frontend, which is not supposed to need all global parameters.
For code usability and a little bit bandwidth, it's better to split apart the parameters being sent
If you really want all parmeters being passed, then just choose another view template instead of the partial one.
我不是 OOP 大神,但我使用了装饰器模式来构建视图(因为我不喜欢 codeigniter/zend 等中的模板,所以使用了我自己的视图)。
就构建页面而言,(在我看来)重要的是部分视图能够与父级进行通信 - 如果部分视图的组件需要一些外部资源(例如特定的 javascript 文件),它应该能够告诉布局将其包含在页面的标题部分,而无需访问本身来进行更改。
因此,我认为部分视图不应该访问该视图,但必须与其进行通信,否则您将构建该部分对该视图的依赖关系,这可能会阻止其中一个或两个视图在其他地方使用。
I'm no OOP god but I have used the decorator pattern to build views (Rolled my own as I don't like the templates in codeigniter/zend etc).
In terms of constructing a page it is (in my opinion) essential that the partial views are able to communicate with parents - if a component of your partial view requires some external resource (like a specific javascript file) it SHOULD be able to tell the layout to include it in the head section of a page without having access itself to make that change.
So I suppose the partial view should NOT have access to the view but must communicate with it, otherwise you are building in a dependency for that partial to that view which may prevent one or both being utilised elsewhere.
一般来说,最好只向需要正确渲染的视图(或者在本例中为部分视图)提供数据。
这迫使开发人员在修改任何组件之前要考虑一下,并防止视图/部分视图之间的职责混合。这也增强了可重用性,因为您不会冒部分视图需要访问可能不存在于使用部分视图的每种情况下的数据的风险。
In general, it's a good idea to only provide the data to the View (or, in this case, Partial View) that it needs to properly render.
This forces developers to think a bit before modifying any component and prevents the mixing of responsibilities between Views/Partial Views. This also enhances re-usability since you won't run the risk of your Partial View requiring access to data that might not be present in every situation the Partial View is being used in.
Ruby on Rails - 默认答案是否定的。部分视图可以被视为相应视图的子视图。
如果您进行一些异步数据编辑 - vodoo,可能会出现一些问题。
除此之外,保持简单和愚蠢......视图的部分知道视图知道什么......
the Ruby on Rails - default answer is no. The partials can be considered as children of the corresponding view.
There could be some problems if your doing some asynchronous data editing - vodoo.
Other then that keep it simple and stupid.. The partial of a view knows what the view knows...