辅助函数 - 希望 DRYness
我喜欢使用辅助函数的想法,但我在设计方面遇到了一些困难。
我有一个 Javascript 函数,可以将实体添加到列表框 - 看起来或多或少像这样:
function MyEntityAdd(o) {
var li = $('<li />').append(o.Product.Name);
$('#SomeListbox').append(li);
}
...以两种方式调用:1)用于在页面加载时显示项目:
$(function() {
@foreach (var item in Model) {
@: MyEntityAdd({ Product: { Name: '@item.Product.Name' }});
}
}
以及,2)用于添加项目当用户(通过 ajax 调用)创建一个新实体时到现有页面:
$.ajax({ type: 'post',
url: '/MyEntity/Create',
data: { ... },
success: function(o) {
MyEntityAdd(o)
}
});
现在我的问题:我需要格式化产品,并通过辅助函数完成它:
@helper FormatProduct(MyEntity e)
{
<strong>@e.Product.Name</strong> @e.Version
}
所以现在我可以像这样重新定义我的 Js(参数现在是平坦的,不是嵌入的物体对象):
function MyEntityAdd(o) {
var li = $('<li />').append(o.Product);
$('#SomeListbox').append(li);
}
像这样调用:
$(function() {
@foreach (var item in Model) {
@: MyEntityAdd({ Product: '@Html.FormatProduct(item.Product)' });
}
}
......一切都很好。除了现在 success
ajax 调用不起作用,因为 Create
操作返回一个 JSON 对象...所以我必须在控制器 ie 内格式化产品返回:
[HttpPost]
public ActionResult Create(string Name)
{
MyEntity e = new MyEntity(Name);
db.MyEntities.Add(e);
db.SaveChanges();
return Json(e);
}
我还必须在这一端复制格式化功能:
[HttpPost]
public ActionResult Create(string Name)
{
MyEntity e = new MyEntity(Name);
db.MyEntities.Add(e);
db.SaveChanges();
return Json(new { Product = MyServerSideFormattingFunction(e) });
}
这很奇怪(不干燥)。给任何人带来灵感吗?
I love the idea of using helper function but I'm struggling a little with design.
I have a Javascript function that can add an entity to a listbox - looks more or less like this:
function MyEntityAdd(o) {
var li = $('<li />').append(o.Product.Name);
$('#SomeListbox').append(li);
}
...which gets called in two ways: 1) for displaying items when the page gets loaded:
$(function() {
@foreach (var item in Model) {
@: MyEntityAdd({ Product: { Name: '@item.Product.Name' }});
}
}
and, 2) for adding an item to the existing page when the user (through an ajax call) creates a new entity:
$.ajax({ type: 'post',
url: '/MyEntity/Create',
data: { ... },
success: function(o) {
MyEntityAdd(o)
}
});
Now my problem: I need to format product and I accomplish it via a helper function:
@helper FormatProduct(MyEntity e)
{
<strong>@e.Product.Name</strong> @e.Version
}
so now I can redefine my Js like this (the parameter is now flat, not an object with embedded objects):
function MyEntityAdd(o) {
var li = $('<li />').append(o.Product);
$('#SomeListbox').append(li);
}
to call like this:
$(function() {
@foreach (var item in Model) {
@: MyEntityAdd({ Product: '@Html.FormatProduct(item.Product)' });
}
}
...all nice. Except that now the success
ajax call doesn't work because the Create
action returns a JSON object... so I'd have to format the product inside the controller i.e. instead of returning:
[HttpPost]
public ActionResult Create(string Name)
{
MyEntity e = new MyEntity(Name);
db.MyEntities.Add(e);
db.SaveChanges();
return Json(e);
}
I'd have to replicate the formatting functionality at this end as well:
[HttpPost]
public ActionResult Create(string Name)
{
MyEntity e = new MyEntity(Name);
db.MyEntities.Add(e);
db.SaveChanges();
return Json(new { Product = MyServerSideFormattingFunction(e) });
}
which is eeky (not DRY). inspirations anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的质疑是对的。就我个人而言,我喜欢 DRY,但我看到的关于您完全不喜欢的最终想法的最糟糕的事情是您将在控制器中添加 HTML,这从根本上来说是不好的做法,因为这是您的视图的工作。
我认为辅助函数很棒,但虽然它们只是服务器端,但它们并不是客户端使用的正确工具。
因此,我会将您的 MyEntityAdd 函数重写为:
一切都应该像使用辅助方法之前一样就位,当然它是 DRY 的。有时最简单的方法是最好的:)
You are right to question this. Personally I love DRY but the worst thing I see about the final idea which you quite rightly dislike is that you will be adding HTML in your Controller which is fundamentally bad practice since that is the job of your Views.
I think helper functions are great but while they are only server side they are not the correct tool for your client side use.
So I would rewrite your MyEntityAdd function to be:
And everything should fall into place as it was before you used the helper method and it is DRY of course. Sometimes the simplest ways are the best :)