同一母公司下2看似独立意见应如何互动 [英] How should 2 seemingly independent views under the same parent interact

查看:84
本文介绍了同一母公司下2看似独立意见应如何互动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情形:

<ParentView>
     <FilterSubview></FilterSubview>
     <ListSubview></ListSubview>
</ParentView>

要为您和例子:我有一个观点反过来显示视图带过滤器(用户可以选择显示书籍,杂志或两者),并与项目名单。
这两个过滤器和清单都有相应的机型。过滤器 - 我们能进行过滤。列表 - 所有的产品清单

To give you and example: I have a view which in turn shows view with filter (user can select to display books, magazines or both of them) and the list with items. Both filter and list have corresponding models. Filter - what can we filter. List - list of all items.

使用案例:用户看到的完整列表,然后就可以通过只选择所需的类别过滤结果

Use case: user sees the full list and then can filter results by selecting only desired category.

问题:


  • 如何这两个观点应该互动?他们应该知道对方还是应该父视图处理呢?

  • 谁应该存储过滤列表中显示?它可以直接将列表子视图模型或父视图可以过滤完整列表,然后通过它来渲染。

推荐答案

有没有一个正确的答案,你的问题,但我会尽力在这里解释一个共同的,惯用的方式。

There is no one correct answer to your questions, but I'll try to explain a common, idiomatic way here.

两兄弟的观点应该不知道对方。相反,他们应该通过某种调解员通过事件进行交互。因为在你的情况下,两个 FilterView ListSubView 分享其负责呈现他们两个,你可以一个共同的父视图让父视图调解事件:

Two sibling views should not know of each other. Instead they should interact via events through some kind of a mediator. Since in your case both FilterView and ListSubView share a common parent view which is responsible for rendering both of them, you could let the parent view mediate the events:

var ParentView = Backbone.View.extend({
  initialize: function() {
      this.listenTo(this.filterView, "filter", this.filterChanged);
  },
  filterChanged: function(filterValue) {
      this.listSubView.filter(filterValue);
  }
});

var FilterView = Backbone.View.extend({
  events: {
      "change .filter" : "filterValueChanged"
  },
  filterValueChanged: function() {
      var filterValue = //get filter value...
      this.trigger("filter", filterValue);
  }
});

另外(preferrably,甚至),你可以切出一个中间人,并使用中保格局。对于您需要第三个组成部分,他们的工作是通过谁不应该知道对方的各方之间的消息。如果您在使用0.9.9骨干,还有就是这样建在调解:在骨干根对象可以作为一个全球性的事件总线用于这一目的。

Alternatively (preferrably, even) you can cut out a middle man and use the Mediator pattern. For that you need a third component whose job it is to pass messages between parties who should not know of each other. If you're using Backbone 0.9.9, there's just such a mediator built in: the Backbone root object works as a global event bus for this purpose.

所以:

//ListSubView
this.listenTo(Backbone, "listfilterchanged", this.filterChanged);

//FilterView
Backbone.trigger("listfilterchanged", filterValue);

再有就是谁应该负责列表数据的问题。我倾向于preFER拥有最专业的部分负责,但这样只有有一个组件负责。你的情况这将意味着 ListSubView 应该管理过滤列表,但只有当 ParentView 不需要操作它。这只是一个概括的,所以把它当作一粒盐,做什么,你的情况正确的感觉。

Then there's the question of who should be responsible of the list data. I tend to prefer to have the most specialized component be in charge, but so that only one component is in charge. In your case that would mean that the ListSubView should manage the filtered list, but only if the ParentView doesn't need to operate on it. That's just a generalization though, so take it with a grain of salt and do what feels right for your case.

这篇关于同一母公司下2看似独立意见应如何互动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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