简体   繁体   English

辅助功能-希望干燥

[英]Helper functions - hoping for DRYness

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: 我有一个可以将实体添加到列表框的Javascript函数-看起来或多或少像这样:

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: ...这有两种调用方式:1)在页面加载时显示项目:

$(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: 2)当用户(通过ajax调用)创建新实体时,将一项添加到现有页面中:

$.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): 所以现在我可以像这样重新定义我的Js(参数现在是flat,不是带有嵌入式对象的对象):

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 ie instead of returning: 除非现在success ajax调用不起作用,因为Create动作返回了一个JSON对象...所以我不得不在控制器内部格式化产品,即不要返回:

[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? 有灵感吗?

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. 我个人很喜欢DRY,但是对于您完全不喜欢的最终想法,我看到的最糟糕的事情是,您将在Controller中添加HTML,这从根本上是不好的做法,因为这是View的工作。

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: 因此,我将您的MyEntityAdd函数重写为:

function MyEntityAdd(o) {
    var productDisplay = '<strong>' + o.Product.Name + '</strong> ' + o.Version;
    var li = $('<li />').append(productDisplay);
    $('#SomeListbox').append(li);
}

And everything should fall into place as it was before you used the helper method and it is DRY of course. 并且所有内容都应该像使用辅助方法之前一样放到位,并且当然是DRY。 Sometimes the simplest ways are the best :) 有时最简单的方法是最好的方法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM