MVVM架构~knockoutjs与MVC配合,实现列表的增删改功能

返回目录

MVC与MVVM的模型

在MVC实例项目中,为我们提供了简单的增删改查功能,而这种功能的实现与具体的Model很有关系,或者说它与后台数据库的关系过于紧密了,而对于开发人员来说当页面布局修改后,也会现时修改它们的Model部分,而对于MVVM思想体系来说,它可以不去修改后台Model,而后采用了一种前台绑定的方式,很好的实现了前台模块与后台Model的解耦!

实例代码

本实例主要展现了一个简单的列表操作,包括了添加,删除和修改,在使用knockoutjs实现它之后,使它的用户体验提升了一个台阶,对于代码的分层有了一个新的升华!

C#核心代码

  public ActionResult Index()
{
return View(new List<Product>
{
new Product{ID=,Name="test1"},
new Product{ID=,Name="test2"},
new Product{ID=,Name="test3"},
new Product{ID=,Name="test4"},
new Product{ID=,Name="test5"},
new Product{ID=,Name="test6"},
});
}

JS核心代码

 var People = function () {
this.ID = 0;
this.Name = "";
}
var model = function () {
self = this;
self.PeopleList = ko.observableArray(@Html.Raw(Json.Encode(Model)));
self.remove = function (o) { self.PeopleList.remove(o); };
self.selectItem = ko.observable();
self.editing = ko.observable(false); self.add = function (o) {
self.editing(true);
self.selectItem(new People());
} self.edit = function (o) {
self.editing(true);
self.selectItem(o);
} self.selectItem.subscribe(function (o) {
// alert("要编辑记录ID是" + o.ID);
});
self.save = function (o) {
alert((o.ID > 0 ? "修改数据" : "新建数据") + o.Name);
}
self.cancle = function () {
self.editing(false);
}
}

HTML核心代码

<h3>
<a href="javascript:void(0)" data-bind="click:add">添加实体</a></h3>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th></th>
</tr>
</thead>
<tbody data-bind="template:{name:'list',foreach: PeopleList}">
</tbody>
</table>
<script type="text/html" id="list">
<tr>
<td><span data-bind="text:ID"></span></td>
<td><span data-bind="text:Name"></span>
</td>
<td><a href="javascript:void(0)" data-bind="click:$parent.remove">删除</a>
<a href="javascript:void(0)" data-bind="click:$parent.edit">编辑</a>
</td>
</tr>
</script>
<fieldset data-bind="with:selectItem,visible:editing">
<legend>正在<span data-bind="if:ID==0">新建</span><span data-bind="if:ID>0">编辑</span></legend>
姓名:
<input type="text" data-bind="value:Name" />
<input type="button" data-bind="click:$parent.save" value="保存" />
<input type="button" data-bind="click:$parent.cancle" value="取消" />
</fieldset>

运行效果图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAE/CAIAAADexs4KAAAWK0lEQVR4nO3dz47jyGHAYb8TnyTMaR8hh2wwNAzMaeCLAwNBgBZgn5LJIUCuGwY5zRPk1kIySU45BHmApv0Em4P+NLtYLJVEqliSv8IHQ0vxT41nrZ+LrZZ+MbwNAFCVX2w+AwAIiBMA1REnAKojTgBUR5wAqI44AVAdcQKgOuIEQHXECYDqiBMA1REnAKojTgBUR5wAqI44AVAdcQKgOuIEQHUWxenf/v2/n8DmfwcABMRJnACqI07iBFAdcRIngOpsGaemaZqmucchwW7pozb/OwAgsFqcxgFoZsbyOGUeJU4AD23NldO0Aekq5GcmZ4gTwNO47229xIIpGL/7u3+I9mbJOimzZJv/HQAQWCdO41f8G9Y34zhdrNrFxVDiQlZOAA/hLj9zOm8JkjNXkcw4TVdFiduA0YWaOAE8hHvd1ru2NOk4ZS6GojkUJ4CHs/IbIv7iL/8qur6J5uGqldPFXM118XDmX//mt3OHbP53AEBg/TjlrJnmEpJz4FXPihPAI7rj7zmdH0R/8pQZpyZ7RBdk5we//s1v56q2+d8BAIG7x+mwlpr7UdANK6e52gVnbmJvT48euPnfAQCBe72VPLjRd/H9DnO/55S+ZXdxn5zbgJv/HQAQWD9OQRUOb5FI9yNn5RQtVvS64gTw6NaM02MNcQKolq/MECeA6oiTOAFUZ1GcAOAexAmA6ogTANURJwCqI04AVEecAKiOOAFQHXECoDriBEB1xAmA6ogTANURJwCqI04AVEecAKiOOAFQHXECoDqrxenw3edz/7i66PnzL3rv6QGwxL3iFN0yd9TcuOpymVe8dk8AylsnTuPX+qtic+32xBVzjkrPcPO/DAAO7hWnxA5LtqevmNOYhUsuAApYIU7jNvz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wAqI44AVAdcQKgOuIEQHXECYDqiBMA1VknTv/6L//8q1/+CACrWCFO//Wf//HHP7wBwFpWiBMArEucAKiOOAFQHXECoDriBEB1xAmA6vw/6ulmzuS/qxwAAAAASUVORK5CYII=" alt="" />

$.when实现方法回调

下面介绍JQ里的一个小功能,$.when($.ajax({})).done().done()....,它可以方便的实现方法的回调,done()代码块相关于ajax里的success节点里的内容,将代码从success节点拿出来,放到done()里,它可以使代码的层次感和可能性更强!

   $.when($.ajax({
url: "/home/getProduct",
dataType: "JSON",
type: "GET",
success: function (data) {
alert(JSON.stringify(data));
}
})).done(function () { alert("哈哈,回调成功了!"); })
.done(function () { ko.applyBindings(new model()); });

返回目录

上一篇:使用C#程序处理PowerPoint文件中的字符串


下一篇:Java OOP中的字符串篇