简体   繁体   English

在Backbone.js中寻找有关MVC的指南

[英]Looking for guidance on MVC in Backbone.js

According to MVC, models are a "pure" data representation. 根据MVC,模型是“纯”数据表示。 You don't want presentational or non-DRY data in there. 您不希望在其中存在演示数据或非DRY数据。 For example if your model has a "comment_count" field, it shouldn't also contain a "use_plural" field so that you can know whether to print "comment" or "comments". 例如,如果您的模型具有“ comment_count”字段,则它也不应包含“ use_plural”字段,这样您就可以知道要打印“ comment”还是“ comments”。

This post has 0 comments
This post has 1 comment
This post has 2 comments
...

If you put that info in the model, it's bad separation of concerns, however, the alternative is to derive that info in the view. 如果将这些信息放在模型中,则关注点分离很差,但是,替代方法是在视图中派生该信息。 For example: 例如:

var FooView = Backbone.View.extend({
  render: function(){
    this.data = this.model.toJSON();
    this.data.use_plural = this.data.comment_count === 1;
    // and fifty more lines like the above
    $(this.el).html(ich['foo_template'](this.data));
  }
});

My question is, there seems to be this need for in-between data that's too view-ish to be part of the model, but too model-ish to be part of the view. 我的问题是,似乎需要中间数据过于视图化而无法成为模型的一部分,但过于模型化而无法成为视图的一部分。 Is that what MVVM addresses? 那是MVVM解决的问题吗? I looked that up but it seemed very Microsoft-tech-stack-specific. 我查了一下,但似乎非常特定于Microsoft技术堆栈。 I was thinking of putting it into a function and calling it from within render() methods: 我正在考虑将其放入函数中并从render()方法中调用它:

function deriveData(model){
  var data = model.toJSON();
  data.use_plural = data.comment_count === 1;
  // and fifty more lines like the above
  return data;
}

var FooView = Backbone.View.extend({
  render: function(){
    this.data = deriveData(this.model);
    $(this.el).html(ich['foo_template'](this.data));
  }
});

var BarView = Backbone.View.extend({
  render: function(){
    this.data = deriveData(this.model);
    $(this.el).html(ich['bar_template'](this.data));
  }
});

Another idea was to have an AbstractView class and inherit from it, like so: 另一个想法是拥有一个AbstractView类并从中继承,如下所示:

var AbstractView = Backbone.View.extend({
  deriveData: function(){
    this.data = this.model.toJSON();
    this.data.use_plural = this.data.comment_count === 1;
    // and fifty more lines like the above
  }
});

var FooView = AbstractView.extend({
  render: function(){
    this.deriveData();
    $(this.el).html(ich['foo_template'](this.data));
  }
});

var BarView = AbstractView.extend({
  render: function(){
    this.deriveData();
    $(this.el).html(ich['bar_template'](this.data));
  }
});

It's not so much that I'm stumped, I just want to make sure I'm not floating along in ignorance while there's this awesome methodology out there that addresses this exact question. 倒不是让我感到难过,我只是想确保我不会在无知中漂浮,而有一种很棒的方法可以解决这个确切的问题。 Does anyone have any recommendations or thoughts, or are there well-established patterns I could leverage here? 是否有人有任何建议或想法,或者在这里可以利用完善的模式? Thanks. 谢谢。

This is a personal opinion (even if it comes from long experience and is shared by many), so take it with a grain of salt. 这是个人观点(即使它来自长期的经验并为许多人所共有),因此请带一点盐。

The MVC pattern just doesn't fit the web really well. MVC模式只是不太适合网络。 And the problem always seem to lie in the Controller . 问题似乎总是在控制器中 Lots of people who've been around for a while know that and accept it silently. 很多已经存在了一段时间的人都知道这一点并默默接受。 Most frameworks use the term MVC as a classification because well, it's convenient to explain in pattern terms what the framework aims at and also most other comparable frameworks use the term. 大多数框架使用术语MVC作为分类,因为很好,可以方便地用模式术语解释框架的目标,并且大多数其他类似框架也使用该术语。

If you really need to attach a meaning to who is the View and who is the Controller in Backbone, most people think they are both the View. 如果您确实需要给谁是View和Backbone中的Controller是一个含义,大多数人都认为它们都是View。 Views generate the template but also dispatch events from the UI to the model and vice-versa. 视图不仅可以生成模板,还可以将事件从UI分发到模型,反之亦然。 If you really really need to keep them separate, you can also think that View classes are Controllers and the templates are the Views . 如果确实需要将它们分开,则还可以认为View类是Controllers ,而模板是Views

Hopefully you get the idea: it does not matter what you call it . 希望您能想到: 叫什么都没关系 It matters though how you use it. 尽管使用方式很重要。

Concerning the practical part of your question: I would use any of these depending on the context. 关于您的问题的实际部分:根据情况,我会使用其中任何一个。 Is it something small you use once? 您一次使用的东西很小吗? Do it inside render . render里面render Is it some logic that happens to be part of your View architecture and is needed in multiple views? 它是否是您的View体系结构的一部分,并且在多个视图中都需要逻辑? Derive from a base class. 从基类派生。 Same way you do with everything else. 处理其他所有事情的方式相同。

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

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