自己構建MVC中的M

來源:互聯網
上載者:User

標籤:

/** * @ 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.