給模型添加ORM,與持久化記錄

來源:互聯網
上載者:User
var Model = {    inherited:function () {    },    created:function () {    },    prototype:{        init:function () {        }    },    //給類添加屬性    extend:function (obj) {        var extended = obj.extended;        for (var i in obj) {            this[i] = obj[i];        }        if (extended) extended(klass);    },    //給實列添加屬性    include:function (obj) {        var included = obj.included;        for (var i in  obj) {            this.prototype[i] = obj[i];        }        //觸發回調        if (included) included(klass);    },    create:function () {        //子類 返回一個新對象,繼承自model對象,建立新模型        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 () {        //返回一個新對喜愛那個,繼承自model.prototype -> model對象的一個執行個體        var instance = Object.create(this.prototype);        instance.parent = this;        instance.init.apply(instance, arguments);        return instance;    }}//儲存資來源物件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];    },    update:function () {        this.parent.records[this.id] = this;    },    save:function () {        this.newRecord ? this.create() : this.update();    },    find:function (id) {        return this.records[id];    }})//繼承的父類model的一個建構函式var Asset = Model.create();//執行個體對象var asset1 = Asset.init();asset1.name = "Aaron-1";asset1.id = 1;asset1.save();var asset2 = Asset.init();asset2.name = "Aaron-2";asset2.id = 2;asset2.save();console.log(    asset1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.