如何在主干应用程序中显示反馈/错误消息

发布于 2024-12-24 04:06:10 字数 816 浏览 1 评论 0 原文

我正在使用 Rails/Backbone/JST 模板进行简单的 CRUD 概念验证。到目前为止,我已经找到了很多例子。但经过大量搜索和阅读后,我还没有找到如何处理这些情况的好例子:

  • 信息消息:新项目成功添加到列表(显示在列表屏幕上)
  • 信息消息:项目成功从列表中删除
  • 错误消息:问题带有字段条目
  • 字段级错误消息:条目问题

Backbone 对象是: 收藏(“后”模型)->模型(“后”对象)->列表/编辑/新视图(以及每个视图的 JST 模板)

因此,我正在寻找有关如何组织代码和模板以实现所需消息传递级别的高级描述。我已经掌握了如何在表单输入发生更改时对其执行验证例程。但现在不知道如何处理错误消息。

这是我正在考虑的方法。不确定这是否是一个好的:

  • 创建一个“消息”模型,它映射到“视图”,它是我现有视图的子视图(如果可能的话)。该视图/模型可以显示我上面提到的前三个场景中的页面级消息和错误。不确定拥有“子视图”是否可行以及如何处理它的模板。但如果可能的话,父模板可以包含“消息”子模板。消息视图可以根据消息模型的状态显示/隐藏子模板。可行的?愚蠢的?
  • 对于第四种情况,每当表单字段更改调用“model.set”时,模型验证将返回一个错误对象,其中包含每个错误字段的特定消息。我不想中断“model.set”,但我确实想在每个字段旁边显示错误消息。我想知道如何以不违反 MVC 模式的方式分解我的编辑/新模板和发布模型/视图。即我不想将对 DOM 元素的引用放在错误的地方。

抱歉,如果这含糊不清。如果您愿意提供帮助,请告诉我哪些代码片段可能有帮助(或其他详细信息),我将提供它们。

I'm working on a simple CRUD proof of concept with Rails/Backbone/JST templating. I've been able to find a lot of examples up to this point. But after much searching and reading, I've yet to find a good example of how to handle these scenarios:

  • info message: new item successfully added to list (shown on list screen)
  • info message: item successfully deleted from list
  • error message: problem with field(s) entry
  • field level error message: problem with entry

The Backbone objects are:
Collection (of "post" Models) -> Model ("post" object) -> List/Edit/New Views (and a JST template for each of these views)

So, I'm looking for a high level description of how I should organize my code and templates to achieve the level of messaging desired. I already have a handle on how to perform my validation routine on the form inputs whenever they change. But not sure what do with the error messages now that I have them.

Here is the approach I'm considering. Not sure if it's a good one:

  • Create a "Message" Model, which maps to a "View", which is a sub-view (if that's possible) on my existing views. This view/model can display page level messages and errors in the first three scenarios I mention above. Not sure if it's feasible to have a "sub-view" and how to handle the templating for that. But if it's possible, the parent templates could include the "message" sub-template. The message view could show/hide the sub-template based on the state of the message model. Feasible? Stupid?
  • For the fourth scenario, the model validation will return an error object with specific messages per each erroneous field each time a "model.set" is called by form field changes. I don't want to interrupt the "model.set" but I do want to display the error message(s) next to each field. I want to know how to factor my edit/new template and Post model/view in such a way that I don't violate the MVC pattern. I.e. I don't want to put references to DOM elements in the wrong plage.

Sorry if this is vague. If you're inclined to help, let me know what code snippets could be helpful (or other details) and I'll provide them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-12-31 04:06:10

您创建一个全局事件总线。当出现错误时会触发一个事件。您应该显示消息的视图监听此事件总线上的事件。这样做,您的错误消息视图不需要知道您的所有集合,反之亦然。事件总线很简单:

var eventBus = _.extend({}, Backbone.Events);

将其添加到您的集合中,并在调用 add 时触发它:

var myCollection = Backbone.Collection.extend({
     initialize: function([],eventbus){
          this.bind('add', function(obj){eventbus.trigger('added', obj)}
      }
})

另请参阅文章:http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

You create a global eventbus. When ever an error appears trigger an event. Your view that should show the message listen to the events on this eventbus. Doing so, your error message view dont needs to know all of your collection and vice versa. The eventbus is simple:

var eventBus = _.extend({}, Backbone.Events);

Add it to your collection and trigger it when ever add was called:

var myCollection = Backbone.Collection.extend({
     initialize: function([],eventbus){
          this.bind('add', function(obj){eventbus.trigger('added', obj)}
      }
})

Take also a look at the article: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文