- 浏览: 114400 次
- 性别:
- 来自: 深圳
最新评论
-
lmh2072005:
hisense731 写道也可以这样就不用报错了。var da ...
new Date(date) -
hisense731:
也可以这样就不用报错了。
var date = "A ...
new Date(date) -
vimest:
知道IE的就已经足够了,opera,chrome,safari ...
再次总结下css的一些hack
转自:http://www.alexatnet.com/content/model-view-controller-mvc-javascript
The article describes an implementation of Model-View-Controller software design pattern in JavaScript. I like JavaScript programming, because it is the most flexible language in the world. With the JavaScript language developers can create applications either in procedural or object-oriented style. It supports almost all programming styles and techniques that I know. I've seen procedural, object-oriented, aspect-oriented JavaScript code snippets. A determined developer can even use functional programming techniques in JavaScript applications. My goal for this article is to write a simple JavaScript component that can show a power of the language. The component is a kind of the HTML ListBox ("select" HTML tag) control with an editable list of items: the user can select item and remove it or add new items into the list. Component will consist of three classes that implement the Model-View-Controller design pattern. I hope, this article can be just a good reading for you, but it would be much better if you consider running the example and adapting it to you needs. I believe you have everything to create and run JavaScript programs: brains, hands, text editor (notepad), and Internet browser (IE or Firefox). The Model-View-Controller pattern, which I'm about to use in the code, requires some description here. As you may know, the name of the pattern is based on the names of its main parts: Model, which stores an application data model; View, which renders Model for an appropriate representation; and Controller, which updates Model. Wikipedia defines typical components of the Model-View-Controller architecture as follows: The data of the component is a list of items, in which one particular item can be selected and deleted. So, the model of the component is very simple - it is stored in an array property and selected item property; and here it is: Event is a simple class for implementing the Observer pattern: The View class requires defining controls for interacting with. There are numerous alternatives of interface for the task, but I prefer a most simple one. I want my items to be in a Listbox control and two buttons below it: "plus" button for adding items and "minus" for removing selected item. The support for selecting an item is provided by Listbox's native functionality. A View class is tightly bound with a Controller class, which "... handles the input event from the user interface, often via a registered handler or callback" (from wikipedia.org). Here are the View and Controller classes: And of course, the Model, View, and Controller classes should be instantiated. The sample, which you can below, uses the following code to instantiate and configure the classes:
/**
* The Model. Model stores items and notifies
* observers about changes.
*/
var
ListModel =
function
(items) {
this
._items = items;
this
._selectedIndex = -1;
this
.itemAdded =
new
Event(
this
);
this
.itemRemoved =
new
Event(
this
);
this
.selectedIndexChanged =
new
Event(
this
);
};
ListModel.prototype = {
getItems :
function
() {
return
[].concat(
this
._items);
},
addItem :
function
(item) {
this
._items.push(item);
this
.itemAdded.notify({item: item});
},
removeItemAt :
function
(index) {
var
item =
this
._items[index];
this
._items.splice(index, 1);
this
.itemRemoved.notify({item: item});
if
(index ==
this
._selectedIndex)
this
.setSelectedIndex(-1);
},
getSelectedIndex :
function
() {
return
this
._selectedIndex;
},
setSelectedIndex :
function
(index) {
var
previousIndex =
this
._selectedIndex;
this
._selectedIndex = index;
this
.selectedIndexChanged.notify({previous: previousIndex});
}
};
var
Event =
function
(sender) {
this
._sender = sender;
this
._listeners = [];
};
Event.prototype = {
attach :
function
(listener) {
this
._listeners.push(listener);
},
notify :
function
(args) {
for
(
var
i = 0; i <
this
._listeners.length; i++) {
this
._listeners[i](
this
._sender, args);
}
}
};
var
ListView =
function
(model, controller, elements) {
this
._model = model;
this
._controller = controller;
this
._elements = elements;
var
_this =
this
;
// attach model listeners
this
._model.itemAdded.attach(
function
() {
_this.rebuildList();
});
this
._model.itemRemoved.attach(
function
() {
_this.rebuildList();
});
// attach listeners to HTML controls
this
._elements.list.change(
function
(e) {
_this._controller.updateSelected(e);
});
};
ListView.prototype = {
show :
function
() {
this
.rebuildList();
var
e =
this
._elements;
var
_this =
this
;
e.addButton.click(
function
() { _this._controller.addItem() });
e.delButton.click(
function
() { _this._controller.delItem() });
},
rebuildList :
function
() {
var
list =
this
._elements.list;
list.html(
''
);
var
items =
this
._model.getItems();
for
(
var
key
in
items)
list.append($(
'<option>'
+ items[key] +
'</option>'
))
this
._model.setSelectedIndex(-1);
}
};
var
ListController =
function
(model) {
this
._model = model;
};
ListController.prototype = {
addItem :
function
() {
var
item = prompt(
'Add item:'
,
''
);
if
(item)
this
._model.addItem(item);
},
delItem :
function
() {
var
index =
this
._model.getSelectedIndex();
if
(index != -1)
this
._model.removeItemAt(
this
._model.getSelectedIndex());
},
updateSelected :
function
(e) {
this
._model.setSelectedIndex(e.target.selectedIndex);
}
};
$(
function
() {
var
model =
new
ListModel([
'PHP'
,
'JavaScript'
]);
var
view =
new
ListView(model,
new
ListController(model),
{
'list'
: $(
'#list'
),
'addButton'
: $(
'#plusBtn'
),
'delButton'
: $(
'#minusBtn'
)
}
);
view.show();
});
<
select
id
=
"list"
size
=
"10"
style
=
"width: 15em"
></
select
><
br
/>
<
button
id
=
"plusBtn"
> + </
button
>
<
button
id
=
"minusBtn"
> - </
button
>
发表评论
-
javascript 判断对象的值是否相等
2012-08-02 10:04 3854经常要判断两个对象的值是否相等 ,写了个方法用来比较,和对象深 ... -
javascript 跟踪效果
2012-03-21 14:06 1060<!DOCTYPE HTML> <html ... -
javascript 判断操作系统
2011-12-12 11:53 1223function checkSys() { //var ... -
js获取地址栏参数
2011-11-18 15:00 1454根据参数获取相应的值 1. func ... -
JSON对象和字符串
2011-11-14 16:43 1648JSON和字符串互转在ajax交互中经常用到,以前用过eval ... -
javascript中的编码和解码
2011-11-11 10:59 15081. encodeURIComponent(url) 函数可把 ... -
鼠标滚轮事件
2011-08-19 17:19 965滚轮事件:IE/chrome/opera/saf ... -
new Date(date)
2011-08-08 12:22 2187在做日历插件的时候遇到的记录一下: var date = & ... -
关于圆角的问题,谁有更好的方式?
2011-07-28 12:36 792现在越来越多的网站喜欢上了圆角,css3 可以很轻松的实现 , ... -
更新下前端相关笔记
2011-07-27 18:57 2361color:red; /* 所有浏览 ... -
获取页面元素的位置
2011-07-26 18:14 973通常需要到获取页面元素相对窗口的位置: func ... -
javascript Object.extend的用法
2011-07-18 18:05 4052javascript Object.extend的用法 ... -
正则表达式笔记
2011-07-14 18:31 875使用RegExp的显式构造 ... -
getElementByClass
2011-07-14 18:20 5684实际应用中很多地方需 ...
相关推荐
CorMVC stands for: Client-Only-Required 一个javascript的MVC框架 Model-View-Controller, and is my laboratory experiment in application architecture held completely seperate from server-side ...
它结合了ASP.NET 的功能强大的功能、Model-View-Controller (MVC) 设计模式以及对测试驱动开发(TDD) 的支持,为开发者提供了更灵活的开发环境。在“Net实战商用源码---ASP.NET MVC多用户博客源码”中,我们可以深入...
**在"14-Model-View-Controller-Tech-Blog"项目中,可能包含对MVC模式的深入讨论,如何在JavaScript环境中应用MVC,以及可能的案例分析和最佳实践。通过学习这个项目,开发者可以提升对于前端架构的理解,掌握如何...
**猫叫声:JavaScript中的Model-View-Controller模式详解** 在软件开发中,Model-View-Controller(MVC)模式是一种广泛应用于创建用户界面的设计模式,它将应用程序的业务逻辑、数据处理和用户交互分离开来,提高...
1. **MVC架构**:MVC模式将应用程序分为三个主要部分——模型(Model)负责数据处理,视图(View)负责显示用户界面,控制器(Controller)处理用户输入并协调模型和视图。这种分离使得代码结构清晰,易于测试和维护...
本项目——"englishroom-final-Realeas-1.rar",是一个利用J2EE技术实现的聊天室应用,它采用经典的Model-View-Controller(MVC)设计模式,旨在提供一个稳定、高效的实时通信环境。 首先,我们要理解MVC架构的核心...
2. **MVC5**:这是一个基于.NET Framework的Microsoft开发的Web应用框架,全称为Model-View-Controller。它将应用程序分为模型、视图和控制器三个部分,提高了代码的组织性和可测试性。在MVC5中,开发者可以利用ASP...
在Spring MVC中,Model代表数据模型,View负责展示数据,Controller处理用户请求并协调Model和View。这个框架的核心设计思想是模型-视图-控制器(MVC)架构模式,旨在实现业务逻辑与用户界面的分离,提高代码的可...
【viper-plugin-jade-mvc】是一个专门为JavaScript开发的插件,主要针对使用Jade(现在被称为Pug)模板引擎的MVC(Model-View-Controller)架构。这个插件致力于提升开发者的效率,简化在Viper框架下使用Jade进行视...
JavaScript 设计模式精讲中,我们探讨了三种重要的软件架构模式:MVC(Model-View-Controller)、MVP(Model-View-Presenter)以及MVVM(Model-View-ViewModel)。这些模式广泛应用于前端开发,特别是在JavaScript...
然而,对于一些较旧的版本,如VS2008,可能并未内置对Model-View-Controller(MVC)框架的支持。MVC是一种设计模式,常用于构建Web应用程序,它将业务逻辑、数据和用户界面分离,使得代码更易于维护和扩展。 标题...
Web development expert Dino Esposito takes you through the web framework’s Model-View-Controller (MVC) design model, and covers the tools you need to cleanly separate business logic from the user ...
ASP.NET MVC5是微软推出的一款开源Web应用程序框架,它基于Model-View-Controller(MVC)设计模式。MVC模式将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller),这有助于实现更好的...
首先,让我们理解MVC(Model-View-Controller)模式。MVC是一种设计模式,它将应用程序的业务逻辑、用户界面和数据模型分离。在ASP.NET MVC中,Controller是处理HTTP请求并决定如何响应的组件,View负责呈现数据,而...
MVC(Model-View-Controller)模式是它的核心设计思想,它将应用逻辑分为三个部分:模型(Model)负责数据处理,视图(View)负责展示,控制器(Controller)处理用户请求并协调模型和视图。 2. jQuery: jQuery ...
You will work with the power of the Model-View-Controller (MVC) pattern on the client, creating a strong foundation for complex and rich web apps. Best-selling author Adam Freeman explains how to ...
MVC是一种软件设计模式,将业务逻辑(Model)、用户界面(View)和控制逻辑(Controller)分离,使代码结构清晰,易于维护。在AMP-PHP框架中,Model负责处理数据,View负责展示,Controller负责接收用户请求并调用...
Web development expert Dino Esposito takes you through the web framework’s Model-View-Controller (MVC) design model, and covers the tools you need to cleanly separate business logic from the user ...
ASP.NET MVC(Model-View-Controller)是一种用于构建可测试、高性能的Web应用程序的开源框架。它采用了MVC设计模式,将应用程序逻辑分为模型、视图和控制器三个部分,使得代码结构清晰,易于维护。MVC5是该框架的一...
MVC模式最早应用于桌面软件开发,现在已广泛应用于Web开发,尤其是JavaScript应用中,它将应用逻辑分为了三个主要部分:模型(Model)、视图(View)和控制器(Controller),使得代码结构清晰,易于维护。...