backbone--model--學習筆記

來源:互聯網
上載者:User

標籤:backbone   backbone筆記   backbone model   backbone模型   

一、模型 - Model

    1.建立一個空的Note模型

        var Note = Backbone.Model.extend({});

    2.執行個體化Note模型

        1).執行個體化一個空的模型

            var note1 = new Note()

        2).執行個體化模型並添加屬性

            var note2 = new Note({

                title: ‘backbone學習筆記‘,

                created_at: new Date()

            });

    3.模型的操作方法

        1).擷取屬性值

            note2.attributes;

            note2.toJSON();  //返回來一個對象

            JSON.stringify(note2); //返回json格式資料

        2).返回對象集合中是否包含指定的key值,包含返回true,否則返回false

            note2.has(‘title‘); 

        3).擷取指定屬性的值

            note2.get(‘title‘);  // 返回title屬性的值

        4).刪除指定屬性的值

            note2.unset(‘title‘);

        5).設定指定屬性的值 有此屬性時,更改值,沒有時添加此屬性和值

            note2.set(‘title‘, ‘backbone學習筆記‘);

        6).清Null 物件中所有的屬性

            note2.clear();

    4.設定模型的預設屬性

        var Note = Backbone.Model.extend({

            defaults: {

                title: ‘‘,

                created_at: new Date()

            }

        });

    5.模型的初始化

        initialize: 此方法在執行個體化時立即調用

        var Note = Backbone.Model.extend({

            defaults: {

                title: ‘‘,

                created_at: new Date()

            },

            initialize: function() {

                console.log(‘新建立了一條筆記:‘ + this.get(‘title‘));

            }

        });

    6.屬性發生變化時,觸發的事件change

        var Note = Backbone.Model.extend({

            defaults: {

                title: ‘‘,

                created_at: new Date()

            },

            initialze: function() {

                console.log(‘新建立了一條筆記:‘ + this.get(‘title‘));


                this.on(‘change‘, function(model, options) {

                    console.log(‘屬性的值發生了變化‘);

                });


                this.on(‘change:title‘, function(model, options) {

                    console.log(‘title 屬性發生了變化‘);

                });

            }

        });

    7.驗證屬性的值 validate

        attributes: 對象屬性

        options: 選項的參數

        var Note = Backbone.Model.extend({

            defaults: {

                title: ‘‘,

                created_at: new Date()

            },


            initialize: function() {

                console.log(‘新建立一條筆記:‘ + this.get(‘title‘));


                this.on(‘change‘, function(model, options) {

                    console.log(‘屬性的值發生了變化‘);

                });


                this.on(‘change:title‘, function(model, options) {

                    console.log(‘title 屬性發生了變化‘);

                });

            },


            validate: function(attributes, options) {

                if (attributes.title.length < 3) {

                    return ‘筆記的標題字元數要大於3‘;

                }

            }


        });

    8.驗證失敗後的事件 invalid

        model:對象

        error:錯誤資訊

        var Note = Backbone.Model.extend({

            defaults: {

                title: ‘‘,

                created_at: new Date()

            },


            initialize: function() {

                console.log(‘新建立了一條筆記:‘ + this.get(‘title‘));


                this.on(‘change‘, function(model, options) {

                    console.log(‘屬性的值發生了變化‘);

                });


                this.on(‘change:title‘, function(model, options) {

                    console.log(‘title 屬性發生了變化‘);

                });


                this.on(‘invalid‘, function(model, error) {

                    console.log(error);   

                });

            },


            validate: function(attributes, options) {

                if (attributes.title,length < 0) {

                    return ‘筆記的標題字元要大於3‘;

                }

            }

        });


本文出自 “network” 部落格,請務必保留此出處http://lipq09.blog.51cto.com/856751/1676749

backbone--model--學習筆記

聯繫我們

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