它是不好的做法,返回包含JavaScript的部分看法? [英] Is it bad practice to return partial views that contain javascript?

查看:66
本文介绍了它是不好的做法,返回包含JavaScript的部分看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个客户数据库,它可以让我搜索用户和公司,浏览和使用ASP.NET MVC和JavaScript(jQuery的)。

I am implementing a customer database which lets me search for users and companies, browse and edit their details, and many other things using ASP.NET MVC and javascript (jQuery).

每当某一职位或得到时,我这样做,通过jQuery.load并插入PartialView到DOM。

Whenever a post or get occurs, I do that via jQuery.load and insert the PartialView into the DOM.

一些局部视图包括形式。我想这是阿贾克斯形式,让那些partialviews有转成形式Ajax表单(通过jquery.form)的document.ready处理程序。

Some partial views include forms. I want those to be ajax forms as well, so those partialviews have document.ready handlers that turn the forms into ajax forms (via jquery.form).

在开始的时候我是在回调/ code,它插入局部视图到DOM处理这个。然而,这导致了含有大量引用了不同的网页JavaScript函数一个大的脚本。我通过插入脚本到相应的页面重构了很大的JavaScript。

At the beginning I was handling this in the callback / code that inserts the partial view into the DOM. However, this resulted in one big script containing lots of javascript functions that refer to different pages. I refactored the big javascript by inserting the scripts into the respective pages.

现在的code是更时尚(我有处理程序要少得多OnXXXPartialView)和code是整齐它属于局部视图中。其中的大部分文件仅包含3-4行JavaScript code,所以开销是不是真的显著。

Now the code is sleeker (I have much less OnXXXPartialView handlers) and the code is neatly inside the partial view it belongs to. Most of those files only include 3-4 lines of javascript code, so the overhead is not really significant.

因此​​,有效地,我不仅将元素添加到文档DOM,但有时我还加入了一段JavaScript代码。实际上,这工作得很好,但似乎<一个href=\"http://stackoverflow.com/questions/858779/making-firebug-break-inside-dynamically-loaded-javascript\">Firebug不能调试动态加载脚本的。

So effectively I am not only adding elements to the document DOM, but sometimes I am also adding a piece of javascript. In practice this works fine, but it seems Firebug can't debug the dynamically loaded scripts.

有ofcourse变通Firebug的问题,但我想知道我是否架构可能是真正的罪魁祸首在这里。你在哪里把属于局部视图的JavaScript?是否有任何的最佳做法?

There are ofcourse workarounds the Firebug issue, but I am wondering whether my architecture may be the real culprit here. Where do you put javascript that belongs to partial views? Is there any best practice?

推荐答案

如果你正在经历包括在你的部分看法的javascript的麻烦,我会切换到只是在你的Ajax调用返回JSON,这样你可以处理它所有的客户端。虽然,我承认我总是preFER此方法。

If you are going through the hassle of including javascript in your partial views, I would switch to just returning JSON in your ajax calls, that way you can handle it all on the client. Though, I will admit I always prefer this method.

至于最佳实践,我一直认为这是不好的Ajax调用,而不是返回JSON生成html,但是那只是我(不敲你,它是一个个人选择)。显然,微软并不认为它不好的做法,因为他们已经专门内置功能来支持它。无论如何,我不会考虑包括你的HTML不仅仅是摆在首位发送HTML回到更糟的JavaScript。

As for best practice, I've always considered it bad to return generated html in an ajax call instead of json, but thats just me (NOT knocking you, its a personal choice). Obviously Microsoft doesn't think its bad practice as they've specifically built in functionality to support it. Anyway, I wouldn't consider including javascript with your html any worse than just sending html back in the first place.

我很好奇,虽然,有什么在JavaScript?

I'm curious though, what's in the javascript?

编辑:
具体而言,我赞成调用Ajax来获得JSON,然后用客户端JS构建局部视图,并将其插入到DOM的。相对于调用Ajax来获得服务器呈现的HTML的客户端,然后插入到DOM。

To be specific, I'm in favor of making an ajax call to get json, then using client side JS to build that "partial view" and insert it into the dom. As opposed to making an ajax call to get server rendered html for the client to then insert into the dom.

一些局部视图包括形式。我想这是阿贾克斯形式,让那些partialviews有转成形式Ajax表单(通过jquery.form)的document.ready处理程序。

Some partial views include forms. I want those to be ajax forms as well, so those partialviews have document.ready handlers that turn the forms into ajax forms (via jquery.form).

我认为你可以在回调/ code,它插入局部视图到DOM处理这个问题。

I would think that you could handle this in the callback / code that inserts the partial view into the dom.

编辑:如果它的有序,高效,良好的组织和工作对你那么我会坚持下去。拥有一切好的和紧凑成局部视图的想法是绝对有吸引力,所以我不会太在意违反任何的最佳做法。我唯一​​担心的是,JS可能被重用,这将是,如果你在那里遍地插入相同的脚本的情况。但在这种情况下,它听起来像你有条块怎么回事,所以我会坚持下去,除非你可以用JS的其余部分概括你的脚本并包含一个很好的协议。

If its orderly, efficient, well organized and works for you then I'd stick with it. The idea of having everything nice and compact into a partial view is definitely appealing so I wouldn't be too concerned with violating any best practices. My only concern was that the JS might be reusable, which would be the case if you where inserting the same script over and over. But in this case it sounds like you have a good deal of compartmentalization going on so I'd stick with it unless you can generalize your scripts and include them with the rest of your JS.

这篇关于它是不好的做法,返回包含JavaScript的部分看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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