mvc-3模型和数据(1)
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)的更多相关文章
- Asp.net MVC 4 模型的数据注释
[Bind(…)] Lists fields to exclude or include when binding parameter or form values to model properti ...
- ASP.NET MVC 模型和数据对象映射实践
在使用 MVC 开发项目的过程中遇到了个问题,就是模型和数据实体之间的如何快捷的转换?是不是可以像 Entity Framework 的那样 EntityTypeConfiguration,或者只需要 ...
- ASP.NET MVC 5 学习教程:通过控制器访问模型的数据
原文 ASP.NET MVC 5 学习教程:通过控制器访问模型的数据 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连 ...
- 【译】ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据
原文:[译]ASP.NET MVC 5 教程 - 6:通过控制器访问模型的数据 在本节中,你将新建一个MoviesController 类,并编写获取电影数据的代码,使用视图模板将数据展示在浏览器中. ...
- ASP.NET MVC数组模型绑定
在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type="text" name ...
- .net的WebForm模拟MVC进行模型绑定,让自己少操劳
一.前言 用过MVC的兄弟们都知道,MVC有模型绑定表单提交的数据功能,那么我也想偷个懒也写个WebForm版的模型绑定.这里主要定义一个泛型方法,然后通过反射把表单上对应属性名字的值赋值到反射创建类 ...
- MVC数组模型绑定
ASP.NET MVC数组模型绑定 在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type=& ...
- MVC 编程模型及其变种
MVC 编程模型及其变种 MVC全称是Model View Controller, 这是一个模型(model)-查看(view)-调节器(controller)缩写,这是通过通用的编程模型非.MVC当 ...
- MVC编程模型
MVC 编程模型 MVC 是三个 ASP.NET 开发模型之一. MVC 是用于构建 web 应用程序的一种框架,使用 MVC (Model View Controller) 设计: Model(模型 ...
- 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
随机推荐
- delphi 枚举类型
枚举类型定义了一系列有序值的集合.枚举变量就是从这个既定的集合中取某个值.集合中的有序值可以称为元素,元素一般从0开始索引(也就是元素的顺序号). 定义一个枚举类型,采用以下的格式: type typ ...
- Python strip、lstrip和rstrip的用法
Python中strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个参数都可以传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是一 ...
- redhat 6 / centos 6 搭建Django环境
1)首先 安装的时候 到 选择安装那些包的时候 把 编译环境和开发的包 那块全部打上勾 2)系统虽然自带Python安装包,但是版本比较低.所以推荐自行进行tar包编译安装比较新的 https:// ...
- 【云计算】K8S DaemonSet 每个node上都运行一个pod
Kubernetes容器集群中的日志系统集成实践 Kubernetes是原生的容器编排管理系统,对于负载均衡.服务发现.高可用.滚动升级.自动伸缩等容器云平台的功能要求有原生支持.今天我分享一下我们在 ...
- C语言指针总结
C语言中的精华是什么,答曰指针,这也是C语言中唯一的难点. C是对底层操作非常方便的语言,而底层操作中用到最多的就是指针,以后从事嵌入式开发的朋友们,指针将陪伴我们终身. 本文将从八个常见的方面来透视 ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]
[本文链接] http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html [题目] 对于一个由N个整数组成的数组,需要比较多少次才能把 ...
- Html标签<a>的target属性
target属性规定了在何处打开超链接的文档. 如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与这个目标吻合的框架或者 ...
- 有时间测试dism
dism /capture-image /imagefile:d\win.win /capturedir:c:\ /name:win81 dism /export-image /winboot /so ...
- 【读书笔记】读《JavaScript设计模式》之观察者模式
一.定义 在事件驱动的环境中,比如浏览器这种持续寻求用户关注的环境中,观察者模式(又名发布者-订阅者(publisher-subscripber)模式)是一种管理人与其任务之间的关系(确切地讲,是对象 ...