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. E. Tetrahedron(数学推导)

    E. Tetrahedron 分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报 time limit per test 2 seconds memory lim ...

  2. 小希的迷宫(MST单棵树判断法则)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

  4. vundle安装 给vim插上翅膀

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) vundle安装方法如下 首先执行以下命令 $ git clone https://githu ...

  5. Linux下tomcat服务

    一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...

  6. backbone杂记

    国人的一个不错的分享:http://gavin.iteye.com/blog/1446277 backbone项目如何组织文件结构 引用: http://bocoup.com/weblog/organ ...

  7. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  8. 一些Linux的路径

    系统引导时启动        /etc/rc.d/rc.local

  9. 【Python】Python AES 对称加密示例

    代码: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex AES_SECRET_KEY = ...

  10. Backpack | & ||

    Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...