"Hello World"在MVC模式中 [英] "Hello World" in MVC Pattern

查看:93
本文介绍了"Hello World"在MVC模式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在接受某公司的采访时,有人问我这个问题.

In an interview for some company, I was asked this question.

您知道什么设计模式...然后,我被告知要编写基于MVC设计模式的最简单的"hello world"应用程序.

What design patterns do you know...then I was told to write simplest "hello world" application based on MVC Design Pattern.

我想出了一个JavaScript程序

I came up with a JavaScript program

var arr = ["a","b","c","d"];   // this is an array, same as store or model
alert(arr[0]);                // this is controller
//and browser alert is a view.

后来我被告知警报是一个视图.我知道有关MVC的基本概念是将对Model的任何更改报告给View.在两者之间有一个控制器来调用方法.

later I was told that alert is a view. The basic concept about MVC I know is any changes in Model are reported to View. And there is a controller in between to call the methods.

您是否可以纠正我的方法,或者为Hello World MVC应用程序提出替代解决方案?还要解释MVC的细微方面.

Can you correct my approach, or come up with an alternate solution for hello world MVC application. Also explain subtle aspects of MVC.

谢谢.

推荐答案

var M = {}, V = {}, C = {};

M.data = "hello world";

V.render = function (M) { alert(M.data); }

C.handleOnload = function () { V.render(M); }

window.onload = C.handleOnLoad;

控制器(C)侦听某种交互/事件流.在这种情况下,这是页面的加载事件.

Controller (C) listens on some kind of interaction/event stream. In this case it's the page's loading event.

模型(M)是数据源的抽象.

Model (M) is an abstraction of a data source.

视图(V)知道如何从模型中渲染数据.

View (V) knows how to render data from the Model.

控制器告诉View对模型中的内容进行处理.

The Controller tells to View to do something with something from the Model.

在此示例中

  • 除实现某些接口外,视图对模型一无所知
  • 模型对视图和控制器一无所知
  • 控制器同时了解模型和视图,并告诉视图对模型中的数据进行处理.

请注意,以上示例是出于演示目的的严重简化.对于JS MVC世界中的 real "hello world"示例,请查看 todoMVC

Note the above example is a severe simplification for demonstrating purposes. For real "hello world" examples in the JS MVC world go take a look at todoMVC

这篇关于"Hello World"在MVC模式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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