自己构建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 服务.另外,还有最新的数据库技术.最新 ...
随机推荐
- $q -- AngularJS中的服务
此 承诺/延迟(promise/deferred)实现 的灵感来自于 Kris Kowal's QCommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action ...
- PHP获取http头信息和CI中获取HTTP头信息的方法
CI中获取HTTP头信息的方法: $this->input->request_headers() 在不支持apache_request_headers()的非Apache环境非常有用.返回 ...
- 2014多校第十场1004 || HDU 4974 A simple water problem
题目链接 题意 : n支队伍,每场两个队伍表演,有可能两个队伍都得一分,也可能其中一个队伍一分,也可能都是0分,每个队伍将参加的场次得到的分数加起来,给你每个队伍最终得分,让你计算至少表演了几场. 思 ...
- [你必须知道的.NET]第三十回:.NET十年(下)
发布日期:2009.05.11 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. /// <summary> /// 本文部分内容,已 ...
- 理解Node.js事件驱动编程
Node.js现在非常活跃,相关生态社区已经超过Lua(基本上比较知名的功能都有nodejs模块实现). 但是我们为何要使用Node.Js?相比传统的webserver服务模式,nodejs有什么优点 ...
- 妙味课堂——HTML+CSS(第一课)
一句话,还记忆不如烂笔头,何况还这么笨,记下笔记,也是记录这一路学习的过程. 妙味课堂第一课并未一味地先讲HTML,而是穿插着CSS讲解,这一点不同于一些其他视频,这一点挺特别的!所以这一课涉及到HT ...
- converntion
One convention that we have is to use the names of fruits and vegetables for variables(only in small ...
- Linux系统调用--getrlimit()与setrlimit()函数详解
http://www.cnblogs.com/niocai/archive/2012/04/01/2428128.html 功能描述:获取或设定资源使用限制.每种资源都有相关的软硬限制,软限制是内核强 ...
- VCC、VDD、VEE、VSS等有关电源标注的区别
Almost all integrated circuits (ICs) have at least two pins which connect to the power rails of the ...
- MongoDB驱动特性检查列表
http://docs.mongodb.org/meta-driver/latest/legacy/feature-checklist-for-mongodb-drivers/ 1. 基本(Essen ...