progressivelly更新在asp.net mvc模式 [英] progressivelly updating model in asp.net mvc

查看:90
本文介绍了progressivelly更新在asp.net mvc模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很长的分贝网络电话,我想填充我的块模式。我们谈论的是asp.net MVC。
我有一个模糊的想法,每次一个新块可用我应该触发model.Bind()
但我不知道该怎么做的。

I have a long db networking call and I want to populate my model in chunks. We are talking about asp.net MVC. I have a vague idea that each time a new chunk is available I should trigger the model.Bind() but I don't know how to do the plumbing between

A)这是在chunks-它使用每一个新块可用一个事件触发时的图案部分的情况下实施提供数据的服务,但它的事件吗?它应持该模型的参考?

a) the service which is providing the data in chunks- it's implemented using the event pattern- each time a new chunk is available an event is triggered, but which event ? It should hold a reference to the model?

二)将被绑定到模型中的数据(我想这不应该是一个bind()的,而是另外还有一些集合)

b) the data which will be bound to the model ( i suppose it should not be an bind(), but an addition to some collection)

c)如果一切都在步骤a和b确定,那么更改将进一步传播到视图没有怎么办?

c) if everything is ok in steps a and b, then the changes will be propagated to the view without further a do?

推荐答案

您可以使用的有一个隐藏的iframe长轮询并从中会吐服务器块传输编码<脚本>如数据可用标记。在这个脚本标签,你可以调用自定义的回调JavaScript函数会照顾格式化的结果。

You could use long polling with a hidden iframe and chunked transfer encoding from the server which will spit <script> tags as data becomes available. In this script tag you could invoke a custom callback javascript function that will take care to format the results.

更新:

由于在评论部分要求下面是使用一个隐藏的iframe长轮询技术的一个样本实现。

As requested in the comments section here's a sample implementation of a long polling technique using a hidden iframe.

让我们假设你有一些模型:

Let's suppose that you have some model:

public class MyViewModel
{
    public string Foo { get; set; }
}

和你有,在块返回此模式,并通知呼叫者一大块可使用事件服务:

and that you have a service that returns this model in chunks and notifies the caller that a chunk is available using events:

public class MyService
{
    public void GetModels(Action<MyViewModel, object> onModelAvailable, object state, Action onComplete)
    {
        Task.Factory.StartNew(x =>
        {
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    onModelAvailable(new MyViewModel
                    {
                        Foo = "foo " + i
                    }, x);
                    Thread.Sleep(1000);
                }
            }
            finally
            {
                onComplete();
            }
        }, state);
    }
}

现在,我们可以有以下控制器:

Now, we could have the following controller:

public class HomeController : AsyncController
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LongPoll()
    {
        var service = new MyService();
        return new MyActionResult(service);
    }
}

和以下观点:

<script type="text/javascript">
    // we define a callback function which will be invoked
    // when a chunk is available from the server
    var callback = function (model) {
        // the model variable passed here will represent the chunk
        $($('<div/>', {
            html: model.Foo
        })).appendTo('#result');
    };
</script>

<iframe style="display:none;" src="@Url.Action("longpoll")"></iframe>
<div id="result"></div>

现在课程的最后一部分是自定义操作的结果,这将做到块传输的实现:

Now the last part of course is the implementation of the custom action result which will do the chunked transfer:

public class MyActionResult : ActionResult
{
    private readonly MyService _service;
    public MyActionResult(MyService service)
    {
        _service = service;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.BufferOutput = true;
        response.ContentType = "text/html";
        var wait = new ManualResetEvent(false);
        _service.GetModels((model, state) =>
        {
            var httpResponse = (HttpResponseBase)state;
            httpResponse.BufferOutput = true;
            httpResponse.ContentType = "text/html";
            var serializer = new JavaScriptSerializer();
            var script = string.Format(
                "<script type=\"text/javascript\">window.parent.callback({0});</script>",
                serializer.Serialize(model)
            );
            httpResponse.Write(script);
            httpResponse.Flush();
        },
        response,
        () =>
        {
            wait.Set();
        });
        wait.WaitOne();
    }
}

这篇关于progressivelly更新在asp.net mvc模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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