首页 > 代码库 > 自己构建MVC中的M
自己构建MVC中的M
/** * @ description Model MVC中M 数据模型 * @ Object * @ public * @ create method IE不支持 */if(typeof Object.create !== ‘fnction‘){ 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。