自己构建MVC中的M
/**
* @ description Model MVC中M 数据模型
* @ Object
* @ public
* @ create method IE不支持
*/ if(typeof Object.create !== 'function'){
Object.create = function(o){
function F(){}
F.prototype = o;
return new F()
}
} Math.guid = function(){
return 'xxxxxxxx-xxxx-4yxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){
var v = Math.random()*16|0,
c = c == 'x' ? v : (v & 0x8),
c = c.toString(16);
return c.toUpperCase()
})
} var Model = {
created : function(){ }, prototype : {
init: function(){
console.log('prototype.init')
}
}, extend : function(o){
for(var i in o){
this[i] = o[i]
}
}, include : function(o){
for(var i in o){
this.prototype[i] = o[i]
}
}, create : function(){
var object = Object.create(this);//object 继承 Model
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype) // object.prototype 继承 Model.prototype
object.created();
return object
}, init : function(){
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance,arguments);
return instance;
}
} /*
*Object 存储实例对象
*/ Model.records = {}; var Asset = Model.create(); Model.include({
init : function(attr){
if(attr){
this.load(attr)
}
},
load : function(attr){
for(var i in attr){
this[i] = attr[i]
}
}
}) Model.include({
newRecords : true, create : function(){
this.newRecords = false;
this.id = this.id || Math.guid();
this.parent.records[this.id] = this.dup();
}, destroy : function(){
delete this.parent.records[this.id]
}, updata : function(){
this.parent.records[this.id] = this.dup();
}, save : function(){
this.newRecords ? this.create() : this.updata()
}, dup : function(){
var o = {};
for(var i in this){
o[i] = this[i]
}
return o;
} }) Model.extend({
find : function(id){
var record = this.records[id]
if(!record) throw('no you need object')
return record.dup()
}, created : function(){
this.record = {}
}
}) Model.extend({
populate : function(values){
this.records = {};
for(var i = 0, len = values.length; i < len; i++){
var record = this.init(values[i]);
record.newRecords = false;
record.id = record.id || Math.guid();
this.records[record.id] = record;
}
}
}) var asset = Asset.init({name : 'xiaohui108'})
自己构建MVC中的M的更多相关文章
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- ASP.NET Core MVC中构建Web API
在ASP.NET CORE MVC中,Web API是其中一个功能子集,可以直接使用MVC的特性及路由等功能. 在成功构建 ASP.NET CORE MVC项目之后,选中解决方案,先填加一个API的文 ...
- 在ASP.NET Core MVC中构建简单 Web Api
Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 ...
- ASP.NET Core MVC 中的 [Controller] 和 [NonController]
前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...
- MVC中使用Entity Framework 基于方法的查询学习笔记 (一)
EF中基于方法的查询方式不同于LINQ和以往的ADO.NET,正因为如此,有必要深入学习一下啦.闲话不多说,现在开始一个MVC项目,在项目中临床学习. 创建MVC项目 1.“文件”--“新建项目”-- ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)
在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...
- MVC中的数据注解和验证
数据注解和验证 用户输入验证在客户端浏览器中需要执行验证逻辑. 在客户端也需要执行. 注解是一种通用机制, 可以用来向框架注入元数据, 同时, 框架不只驱动元数据的验证, 还可以在生成显示和编辑模型的 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(1)
介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...
随机推荐
- ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...
- SPL 全面剖析
SPL 全面剖析 来自百度百科 http://baike.baidu.com/view/1130234.htm?fr=aladdin SPL(Standard PHP Library) IN PHP5 ...
- github and SourceTree初步使用
1.建立连接(一次就行了) 输入你的github名字 2.创建仓库 3.下载仓库 这个时候打开SouceTree就可以看到之前创的仓库了 点击克隆,创建一个文件夹,讲路径设置好 这时候查看本地列表就会 ...
- POJ 1631
#include <iostream> #define MAXN 500005 using namespace std; int T[MAXN]; int binary_search(in ...
- 如何在Windows7(IIS7)环境下安装 PHP
一.安装IIS7 打开(1)[程序和功能],然后点击(2)[打开或关闭Windows功能] 勾选(1)[IIS管理控制台]和(2)CGI,然后点击[确定]按钮,等待安装完成.这个过程可能需要系统安装光 ...
- 利用PHP SOAP实现WEB SERVICE
php有两个扩展可以实现web service,一个是NuSoap,一个是php 官方的soap扩展,由于soap是官方的,所以我们这里以soap来实现web service.由于默认是没有打开soa ...
- C#打开得到路径
FolderBrowserDialog path = new FolderBrowserDialog(); path.ShowDialog(); MessageBox.Show(path.Select ...
- MySQL数据库的基本操作命令
MySQL数据库的基本操作命令 [mysql]mysql 常用建表语句 一.mysql服务操作 net start mysql //启动mysql服务 net stop mysql //停止mysql ...
- QTableView带可编辑进度条
main文件与上一个例子完全一致,也使用QStandardItemModel,关键是有这句:QStandardItem.setEditable(false); 继承QAbstractItemDele ...
- Ubuntu 13.10 安装Qt5
Qt5在Ubuntu的软件中心是找不到的,只能从Qt的官网下载安装. http://qt-project.org/downloads,选择完整的Linux版本(qt-linux-opensource- ...