返回包含 javascript 的部分视图是不好的做法吗? [英] Is it bad practice to return partial views that contain javascript?

查看:24
本文介绍了返回包含 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).

每当发生 post 或 get 时,我都会通过 jQuery.load 执行此操作并将 PartialView 插入到 DOM 中.

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

一些局部视图包括表单.我也希望那些是 ajax 表单,因此这些部分视图具有将表单转换为 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).

一开始我在回调/代码中处理这个问题,将局部视图插入到 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.

现在代码更流畅(我有更少的 OnXXXPartialView 处理程序)并且代码整齐地位于它所属的局部视图中.大多数这些文件只包含 3-4 行 javascript 代码,因此开销并不大.

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.在实践中这很好用,但似乎 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.

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,这样您就可以处理它一切都在客户端.不过,我承认我一直更喜欢这种方法.

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 中包含 javascript 比首先发送 html 更糟糕.

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.

一些局部视图包括表单.我也希望那些是 ajax 表单,因此这些部分视图具有将表单转换为 ajax 表单的 document.ready 处理程序(通过 jquery.form).

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).

我认为您可以在将局部视图插入 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天全站免登陆