MVC和命名空间

var User = function(atts) {
this.attribute = atts || {};
}
//和具体user相关的方法
User.prototype.destroy = function() {};
//和具体user不相关的函数和变量
User.fetchRemove = function() {};
var user = new User({name:'jinks'});
user.destroy();

构建对象关系映射

如:

  • 任何model的改变会向后台发起一个ajax请求
  • model和view绑定,当一个实例改变时,马上从view中反映出来

原型继承

  • 这里用到Object.create,对于没有的浏览器,可以模拟方法
if(typeof Object.create !== "function") {
Object.create = function(o) {
function F(){};
F.prototype = o;
return new F();
}
}
var Model = {
inherited: function() {},
created: function() {},
prototype: {
init: function() {}
},
create: function() {
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);
object.created();
this.inherited(object);
return object;
},
init: function() {
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance, arguments);
return instance;
}
}
var Asset = Model.create();
var User = Model.create();
var user = User.init();

添加ORM属性

var Model = {
/*代码片段*/
extend: function(o) {
var extended = o.extended;
for(var i in o) {
this[i] = o[i];
}
if(extended) extended(this);
},
include: function(o) {
var included = o.included;
for(var i in o) {
this.prototype[i] = o[i];
}
if(included) included(this);
}
}
//添加到Model对象属性
Model.extend({
find: function() {}
});
//添加Model.prototype中,所有实例共享这一属性
Model.include({
load: function(atts) {
for(var i in atts) {
this[i] = atts[i];
}
},
init: function(atts) {
if(atts) this.load(atts);
}
}); var Asset = Model.create();
var asset = Asset.init({name: "a.png"});

持久化记录

将引用保存至新创建的实例中以便任何时候都可以访问

var Model = {
/*代码*/
}
//用来保存资源的对象
Model.records = {};
Model.include({
newRecord: true,
create: function() {
this.newRecord = false;
this.parent.records[this.id] = this;
},
destroy: function() {
delete this.parent.records[this.id];
}
});
//更新一个已经存在的实例
Model.include({
update: function() {
this.parent.records[this.id] = this;
}
});
//添加一个快捷函数来保持实例
Model.include({
save: function() {
this.newRecord ? this.create() : this.update();
}
});
Model.extend({
//通过id查找,找不到抛出异常
find: function(id) {
var id = this.records[id];
if(id) {
return id;
} else {
throw new Error("Unknow record");
}
}
}); var Asset = Model.create();
var asset = Asset.init();
asset.name = "same, same";
asset.id = 1;
asset.save(); var asset2 = Asset.init();
asset2.name = "but different";
asset2.id = 2;
asset2.save(); console.log(Asset.records);
asset2.destroy();
console.log(Asset.records);

增加ID支持

使用全局统一标识(GUID)生成器来自动生成ID

//使用Math.random()来产生一个伪随机数的GUID
Math.guid = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c){
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16)
}).toUpperCase();
};
//修改create()函数
Model.include({
create: function() {
if(!this.id) this.id = Math.guid();
this.newRecord = false;
this.parent.records[this.id] = this;
}
}); var Asset = Model.create();
var asset = Asset.init();
asset.save();
console.log(asset.id);

mvc-3模型和数据(1)的更多相关文章

  1. Asp.net MVC 4 模型的数据注释

    [Bind(…)] Lists fields to exclude or include when binding parameter or form values to model properti ...

  2. ASP.NET MVC 模型和数据对象映射实践

    在使用 MVC 开发项目的过程中遇到了个问题,就是模型和数据实体之间的如何快捷的转换?是不是可以像 Entity Framework 的那样 EntityTypeConfiguration,或者只需要 ...

  3. ASP.NET MVC 5 学习教程:通过控制器访问模型的数据

    原文 ASP.NET MVC 5 学习教程:通过控制器访问模型的数据 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连 ...

  4. 【译】ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据

    原文:[译]ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据 在本节中,你将新建一个MoviesController 类,并编写获取电影数据的代码,使用视图模板将数据展示在浏览器中. ...

  5. ASP.NET MVC数组模型绑定

    在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type="text" name ...

  6. .net的WebForm模拟MVC进行模型绑定,让自己少操劳

    一.前言 用过MVC的兄弟们都知道,MVC有模型绑定表单提交的数据功能,那么我也想偷个懒也写个WebForm版的模型绑定.这里主要定义一个泛型方法,然后通过反射把表单上对应属性名字的值赋值到反射创建类 ...

  7. MVC数组模型绑定

    ASP.NET MVC数组模型绑定   在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type=& ...

  8. MVC 编程模型及其变种

    MVC 编程模型及其变种 MVC全称是Model View Controller, 这是一个模型(model)-查看(view)-调节器(controller)缩写,这是通过通用的编程模型非.MVC当 ...

  9. MVC编程模型

    MVC 编程模型 MVC 是三个 ASP.NET 开发模型之一. MVC 是用于构建 web 应用程序的一种框架,使用 MVC (Model View Controller) 设计: Model(模型 ...

  10. 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. xcode6以后, 使用.pch

    http://blog.csdn.net/lihuiqwertyuiop/article/details/39268101 总结: . 创建.pch文件 . Apple LLVM 6.1 - Lang ...

  2. UITableview reloadData Animation 动画效果

    http://blog.kingiol.com/blog/2013/10/22/uitableview-reloaddata-with-animation/ 运用到UITableview进行重新加载数 ...

  3. 在ubuntu 15.04下安装VMware Tools

    提出问题:在Ubuntu 15. 04版本上,不能实现剪贴板的共享 解决方法:发现没有装VMware Tools 安装VMware Tools步骤 1. 点击菜单栏,虚拟机 → 安装VMware工具 ...

  4. linux expect 简单讲解

    来自http://blog.csdn.net/winstary/archive/2009/08/08/4422156.aspx使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明, ...

  5. python如何获取某模块的版本信息

    1)module.__version__ 2)用dir(module)查看有没有版本信息 3)help(module)

  6. c++ const总结

    [本文链接] http://www.cnblogs.com/hellogiser/p/cplusplus-const-summay.html 看到const 关键字,C++程序员首先想到的可能是con ...

  7. 22.整数二进制表示中1的个数[Get1BitCount]

    [题目] 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. [分析] 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数 ...

  8. 在64位的linux上运行32位的程序

    1.症状 (1)执行bin文件时提示:No such file or directory (2)ldd bin文件  的输出为: not a dynamic executable (3)file bi ...

  9. iOS 在使用UINavigationController和TabBarController时view的frame

    可能是以前记错了,总认为在ios6上使用了UINavigationController或者TabBarController会因为多了bar而影响子controller的view的frame大小.今天在 ...

  10. struts标签--logic总结

    1. logic:empty 该标签是用来判断是否为空的.如果为空,该标签体中嵌入的内容就会被处理.该标签用于以下情况: 1)当Java对象为null时: 2)当String对象为"&quo ...