Grails查看渲染。(Grails View Rendering. Render the GSP or use backbone + templating?)

编程入门 行业动态 更新时间:2024-10-24 11:15:49
Grails查看渲染。(Grails View Rendering. Render the GSP or use backbone + templating?)

我想使用Grails创建Web应用程序。

主要布局由顶部,左侧,内容等几个部分组成,我希望每个部分都可以部分刷新。 我打算用jQuery来执行ajax请求和刷新。

让我们关注内容布局。 我有两个选项来渲染视图:

我们知道,Grails默认使用Sitemesh作为其视图。 基本上,我将使用jQuery来使用ajax获取gsp页面。 并将该页面放入内容布局中。 我将使用jQuery从Grails控制器获取JSON对象(而不是GSP),并使用任何jquery模板插件使用渲染视图。 我可能需要使用backbone.js / spine.js来管理OO结构。

两个选项中哪一个更好?

我主要考虑以下参数:易于开发,性能,可持续性等。但如果您认为还有其他重要参数需要考虑,请告诉我。 我不是一个经验丰富的Web开发人员。

或许你知道其他更好的选择吗?

谢谢 :)

I want to create web application using Grails.

The main layout consists of few parts, like top, left, content, etc. and I want each part to be partially refreshable. I'm planning to use jQuery to perform ajax request and refresh.

Let's focus on the content layout. I have two options in mind for rendering the view:

As we know, Grails by default using Sitemesh for its view. So basically, I will use the jQuery to get the gsp page using ajax. And put that page into the content layout. I will use jQuery to get JSON object (instead of GSP) from Grails controller, and use render view using any jquery templating plugin. I might need to use backbone.js/spine.js to manage the OO structure.

Which one of the two options is better?

I mostly consider the following parameters: easy to develop, performance, mantainability, etc. But if you think there are other importants parameters to consider, pls let me know. I'm not an experience web developer.

Or perhaps you know other better options?

Thanks :)

最满意答案

首先,可能存在这样的情况:您被迫使用两者的组合(对于相同的结构)。 显然,这种泄漏的可维护性应该避免。

可维护性灵活性开发速度来看,您应该考虑第一种选择。 这就是grails的用途。 这有很大的优势:当第一次调用GSP时,您可以将模板渲染到GSP中。 调用AJAX操作时,您只需重新渲染此模板。 它简短,简单而且有效。 最grails标记可以处理这些操作。 此外,它很容易测试

但是,当您希望根据其他事件(如WebSockets / comet)更新内容并且您不希望/无法再次调用服务器时,使用此方法几乎不会丢失可重用性 。 在这些情况下,您应该使用jQuery-Template实现模板逻辑,并使用从服务器推送消息接收的JSON数据填充它。

性能 :还有一些用例,你也应该考虑第二个选项:如果你有聊天,你只想逐步显示新消息(或者你需要减少服务器使用的带宽 )并且不要我想将完整的聊天进度发送给用户只是因为有一条小的新消息。 在这里,您不能使用没有一些黑客或限制的模板。 此外,如果您想为用户提供实时感觉,您不能让他等待服务器的答案,您需要使用JQuery直接更新视图,您无法重用模板。 因此,您应该将模板数据放在一个位置,并使用jQuery-Template或类似的 - 至少从我的角度来看。

First of all, there might be situations, where you are forced to use a combination of both (for the same structures). Obviously this leaks of maintainability and should be avoided.

From a maintainability, flexibility or speed of development of view, you should consider the first option. This is what grails is done for. There are great advantages for this: You can render your template into the GSP, when the GSP is called first time. When an AJAX action is called you just re-render only this template. It's short, simple and works. The most grails tags can handle those actions. Additionally it is easily testable.

However you lose little reusability with this approach, when you want to update your content based on other events (like WebSockets/comet) and you do not want/cannot to call the server again. In these case you should implement the template logic using jQuery-Template and filling it with JSON data received from your server-push message.

Performance: There are also use-cases, in which you should consider the second option as well: If you have a chat, you only want to display the new messages incrementally (or you need to reduce the used bandwith of server) and don't want to send the complete chat-progress to the user just because there is one small new message. Here you cannot use templates without some hacks or limitations. Additionally if you want to provide realtime feeling to the user, you cannot let him wait for the answer of the server and you need to update the view directly using JQuery, where you cannot reuse your template. Therefore you should have your template data in one place and use jQuery-Template or similar exclusively - at least from my point of view.

更多推荐

本文发布于:2023-08-03 00:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rendering   View   Grails   Render   templating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!