简体   繁体   English

模型在MVC文本编辑器方案中的作用

[英]Role of model in MVC text editor scenario

I'm designing a movie script editing text editor for fun, and to try learn something about low level UI design. 我正在设计一个电影脚本编辑文本编辑器来娱乐,并尝试学习有关低级UI设计的知识。 The format of a screenplay is quite simple, and is by long-standing standard written solely in 12pt courier. 剧本的格式非常简单,并且采用长期标准,仅以12pt速递编写。 So I get the impression that designing an editor would be fairly straightforward, having only one font and font size, and no special formatting. 因此,我的印象是设计编辑器将非常简单,只有一种字体和字体大小,而没有特殊格式。

I'm keen on preserving semantic information -- eg, I want the scene header to be stored as a scene header, not just as text formatted as a scene header. 我热衷于保留语义信息-例如,我希望将场景标头存储为场景标头,而不仅仅是格式设置为场景标头的文本。

The application is further complicated by the change tracking required in scripts. 脚本中所需的更改跟踪使应用程序更加复杂。 Additional pages should have revision information at the top, therefore the document needs to be stored as a set of pages, with appropriate information identifying revisions. 其他页面的顶部应该有修订信息,因此文档需要存储为一组页面,并带有标识修订的适当信息。

It seems to make sense to keep the document as a separate object from the view, then call methods on a document object to add text etc, based on input in the view. 将文档保留为与视图分离的单独对象,然后根据视图中的输入,调用文档对象上的方法以添加文本等,这似乎很有意义。 However, which object is responsible for the pagination? 但是,哪个对象负责分页? At first guess I would have said the document, but pagination seems like a presentation thing. 乍一看,我会说这份文件的,但分页似乎是一种介绍。 It would also not make sense for the document to use the graphics text API to measure strings and work out how much space they take up. 对于文档而言,使用图形文本API测量字符串并计算出它们占用了多少空间也是没有意义的。 However, if it was up to the view, this is in theory liable to conflict with how it has been stored in the document. 但是,如果可以接受,那么从理论上讲,这很可能与文档中的存储方式发生冲突。 So what would be the best solution? 那么什么是最好的解决方案?

I have plenty of experience in programming, but have had very little to do with the nitty gritty of drawing my own user controls from scratch. 我在编程方面有丰富的经验,但是与从头绘制自己的用户控件的精妙之处无关。 I've also deliberately avoided tying this to a specific language, but I'd most likely be writing in C# or C++. 我还特意避免将其与特定语言联系在一起,但是我很可能是用C#或C ++编写的。

Thanks for your time. 谢谢你的时间。

Neither the view or model IMO is responsible for pagination. IMO的视图或模型都不对分页负责。 That's the job of the controller in the MVC paradigm. 这就是MVC范例中控制器的工作。 Sure you would have some thing to represent the pager in the view, but the actual job of the controller to control how many records get sent to the view. 当然,您将有一些东西可以代表视图中的寻呼机,但是控制器的实际工作是控制向视图发送多少记录。

Of course the pager class itself is a model, but the instantiation of it should lie in the controller. 当然,寻呼机类本身就是一个模型,但是它的实例化应该在控制器中。

However, if it was up to the view, this is in theory liable to conflict with how it has been stored in the document 但是,如果可以接受,那么从理论上讲,这很可能与文档中的存储方式产生冲突

Why? 为什么? The document is data, with some semantic information. 该文档是数据,带有一些语义信息。 How much of that data you display on a page is not something you want to store in the document. 您在页面上显示的数据量中有多少不希望存储在文档中。

Keep your document completely ignorant of page numbers, page sizes, or what contents go on a page, and you'll be able to keep all your presentation logic in your view. 使您的文档完全不知道页码,页面大小或页面上的内容,并且可以将所有演示逻辑保持在视图中。

Think of the relationship between HTML and your browser - the HTML knows nothing about pages, how it's viewed, how it's printed, and so on. 考虑一下HTML与浏览器之间的关系-HTML对页面,如何查看,如何打印等一无所知。

If the concept of a "page" is, in fact, data that needs to remain persisted with the rest of the data regardless of how it's viewed, then I would vote that it goes in the model. 如果“页面”的概念实际上是无论数据如何查看都需要与其他数据保持一致的数据,那么我会投票赞成将其放入模型中。 It's unorthodox, that much is certain. 这是非正统的,可以肯定的是。 But you're implying in your description that "pages" are actual entities of the data. 但是您的描述中隐含了“页面”是数据的实际实体。 If that's the case, then a Page model should know everything it needs to know about itself, including how much text it can fit (which shouldn't be terribly difficult given that your data model assumes/requires a specific font for display... which will need to be enforced from within the model somehow, and I'm not sure how). 如果是这样的话,那么页面模型应该知道它需要了解的有关自身的所有信息,包括它可以容纳多少文本(考虑到您的数据模型采用/需要特定的字体来显示,这应该不会很困难...需要从模型内部以某种方式强制执行,而我不确定如何执行)。

It doesn't sound like the view is really deciding how to display the pages, but that the requirements of the display are business logic in this case. 听起来好像视图并没有真正决定如何显示页面,但是在这种情况下,显示的要求是业务逻辑。 The view is just handling the actual display according to those requirements. 该视图只是根据这些要求处理实际显示。

The standard thought on the subject is that "if it's about display, it doesn't belong in the model." 关于该主题的标准思想是“如果是关于显示,则它不属于模型”。 But every rule has an exception. 但是每条规则都有例外。 If the data being stored is typesetting data, then the model has little choice but to know about something that would intuitively feel like display. 如果要存储的数据是排版数据,那么该模型别无选择,只能了解一些直观感觉像是显示的东西。 You'll want to document it well in comments and such, but this sounds like a good exception case to the intuitive rule. 您可能希望在注释等中很好地记录下来,但这听起来像是直观规则的一个很好的例外情况。

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

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