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快速入门之安全策 ...
随机推荐
- 2维特征Feature2D(转)
转自:http://blog.csdn.net/yang_xian521/article/details/6901762 主要介绍的是如何用SURF进行特征匹配,和SIFT的使用方法基本一致.
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- PHP表单验证
<!DOCTYPE html> <html> <head> <title>Test Code</title> </head> & ...
- 解决kettle配置文件中的中文乱码
在日常开发中有时候配置文件会出现中文(如config.properties 里有中文),为了避免出现乱码,因而要转成unicode编码. 1.在设置变量的javascript(转换中的JavaScri ...
- swift 中delegate的使用
今天写了delegate,遇到以下问题: 这里protocol的写法有问题,如果delegate指向一个实现了某个协议对象的引用,在oc里是这样写delegate的类型 id<protocol& ...
- Java如何读取XML文件 具体实现
转载自:http://www.jb51.net/article/44338.htm import java.io.*; import javax.xml.parsers.DocumentBuilder ...
- PHP--TP框架----把查询到的数据,显示在模型(模板)里面
MainController.class.php <?php namespace Home\Controller; use Think\Controller; class MainControl ...
- 膜拜(codevs 3369)
3369 膜拜 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 神牛有很多…当然…每个同学都有自己衷心膜拜的 ...
- Hadoop 1.1.2 eclipse plugin 编译 win7 集成
Windows平台上使用ANT编译Hadoop Eclipse Plugin 一.准备工作: 1.安装JDK 下载页面:http://www.oracle.com/technetwork/java ...
- /bin/dd if=/path/to/source-file of=/path/to/backup-file
[root@ok virhost]# qemu-img info 05t.img image: 05t.img file format: raw virtual size: 10G (10737418 ...