如何封装的可重复使用"控制"对于ASP.NET MVC 3 +剃刀意见 [英] How to encapsulate a reusable "control" for ASP.NET MVC 3 + Razor views

查看:101
本文介绍了如何封装的可重复使用"控制"对于ASP.NET MVC 3 +剃刀意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找关于如何创建几个MVC 3次可重复使用的控制权使用的最佳实践。我可以做一个HTML辅助扩展方法(编程或在剃刀声明佣工)或者我可以创建一个局部视图。

I am looking for best practices on how to create a reusable "control" for use on several MVC 3 views. I could make an Html helper extension method (programmatically or with the declarative helpers in razor) or I could create a partial view.

这一招,在我的情况是,我需要做的不仅仅是转储$ P $一些HTML pcisely该视图调用助手/部分现货。除了把一些HTML标记在现场,我还需要添加一些JavaScript code,使其工作。通常情况下,我把这个code其他页面(例如底部)。当然,这是严格不是必需的。此外,请注意,JavaScript已经无法控制的实例具体。换句话说,有可能写javascript该查找控制HTML标记的所有实例页和激活,它具有适当的事件,等等。

The trick, in my case is that I need to do more than just dump some HTML in precisely the spot that the view calls the helper/partial. In addition to putting some HTML markup in that spot, I also need to add some javascript code to make it work. Normally, I would put this code elsewhere in the page (e.g. the bottom). This is of course strictly not required. Also, note, the javascript is not control instance specific. In other words, it is possible to write javascript that finds all instances of the control HTMl markup on a page and "activates" it with appropriate events, etc.

我已经想到了几种可能性。

I have thought of several possibilities.


  1. 有帮手/部分转储出HTML和<脚本> 在它被称为地方标记权。这似乎是一个坏主意,因为这意味着控制只能在每个网页上使用一次。

  2. 有两个帮手。一个输出HTML标记,第二个是吐出的JavaScript。第二个将被称为有史以来唯一一次,这样脚本在适当的地方最终会被从页面的底部调用。如果我发明了一个第二控制,这是这个样子,我会4佣工(2 HTML,2为Javascript)。

  3. 发明某种的ScriptManager的控制可以用注册的JavaScript。使用ScriptManager将有某种将被添加到页面底部的帮手,并会dump出来的JavaScript了登记脚本的一些片断所有控件。到ScriptManager的帮手呼叫可能在_layout.cshtml,以便它需要它的任何页面上自动发生。这将一筹莫展的时候,一个页面并不需要它。这似乎并不十分MVC十岁上下,我猜。

  4. 只是有吐出来的是HTML的助手,然后加入JavaScript是包含在每一页上的site.js。 JavaScript的将是足够聪明,不要做任何事情,如果它不能在网页上找到任何相关的标记(标记将有一个特定类的包装元素)。会有这种搜索在所有网页上的标记,虽然开销,其中包括不具有任何这些控件的页面。

  5. 同#4,但是把JavaScript的在自己的js文件,包括它只能在使用该控件的页面。这对于那些不使用该控件的页面更有效,但它是一个额外的.js文件和相关的HTTP请求该做的网页。

  1. Have the helper/partial dump out the HTML and a <script> tag right in the place it is called. This seems like a bad idea since it means the control could only be used once per page.
  2. Have two helpers. One to output the HTML markup, and a second that spits out the javascript. The second would be called only ever once and would be called from the bottom of the page so that the script ends up in the right place. If I invented a second control that was like this, I would have 4 helpers (2 for HTML, 2 for Javascript).
  3. Invent some kind of ScriptManager that controls can register javascript with. The scriptmanager would have some kind of helper that would be added to the bottom of pages and would dump out the javascript for all controls that had registered some snippet of script. The call to the scriptmanager helper could be in the _layout.cshtml so that it automatically happens on any page that needs it. It would do nothing when a page didn't need it. This doesn't seem very MVC-ish, I guess.
  4. Just have a helper that spits out the HTML and then add the javascript to the site.js that is included on every page. The javascript would be smart enough to not do anything if it could not find any relevant markup on the page (markup would have a wrapper element with a particular class). There would be overhead of this search for markup on all pages though, including pages that don't have any of these controls.
  5. Same as #4, but put the javascript in its own .js file and include it only on pages that use the control. This is more efficient for pages that don't use the control, but it is an additional .js file and associated HTTP request for pages that do.

我倾向于#4,但是这是一个解决的问题?有没有更好的,第6的选择吗?

I am leaning towards #4, but is this a solved problem? Is there a better, 6th option?

推荐答案

我公司采用MVCContrib便携领域的打包code为可重复使用的组件

My company is employing MVCContrib portable areas to package the code into a DLL for reusable "components"

这些组件中的每一个都可以通过一个扩展方法被调用。例如:

Each of these components can be called via an extension method. For example:

Html.Components().ShowPoll();

在每个这些部件,还有登记被嵌入的资源多的CSS / JS文件的方法。我们的主要网站将提取资源文件和缩小+服务之前,将它们组合。

Within each of these components, there is a way to register multiple css/js files that are embedded resources. Our main site will extract the resource files and minify + combines them before serving it.

该组件也将注册任何在将JQuery的的Document.OnReady事件中被调用页面事件。这允许各组分是迷你站点,具有自己的路由,模型和视图独立功能。

The component will also register any on page event that will be called during the Document.OnReady event of JQuery. This allows each of the components to be mini-sites, standalone functionality with its own routes, models, and views.

在整个网站,同样拉上JS对所有组件供应。之一,因为该文件将被缓存和两个 - 删除确定哪些部件是网页,它需要的资源上的复杂性。

Across the entire site, the same zip up JS for all the components are served. One because the file will be cached, and two - removes the complexity of determining what components are on the page and the resources it needs.

让我知道如果你想关于这个设置的更多信息。

Let me know if you want more information regarding this setup.

这篇关于如何封装的可重复使用&QUOT;控制&QUOT;对于ASP.NET MVC 3 +剃刀意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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