Create a data model using object-oriented

Source: Internet
Author: User

Let's take a look at the following code to understand the object orientation. The information here is large.

4 * object. create () has recently been added to the ecmascript 5th specification. Some browsers do not support 5 * simulating an object here. the create method solves compatibility issues 6 * object. create: This method has only one parameter, namely the prototype object. 7 * the prototype of the new object is the input parameter. That is, an object is input, and a new object 8 */9 10 if (typeof object. Create! = "Function") {11 object. create = function (o) {12 function f () {} 13 F. prototype = O; 14 return F (); 15} 16} 17 18/** 19 * simple and clear guid Generator for Automatic Generation of IDS, added IDs of 20 */21 math to save instance records. guid = function () {22 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx '. replace (/[XY]/g, function (c) {23 var r = math. random () * 16 | 0; 24 var v = c = 'X '? R: (R & 0x3 | 0x8); 25 return v. tostring (16); 26 }). touppercase (); 27}; 28 29/** 30 * creat a object from here, create an object model. Note that the model here is mounted to the object base class, 31 */32 VAR model = {33 inherited: function () {}, 34 created: function () {}, 35 36 prototype: {37 init: function () {} 38}, 39 // This function returns a new object, this object inherits from model object 40 // we use it to create a model 41 create: function () {42 var object = object. creat E (this); 43 object. parent = This; 44 object. prototype = object. fn = object. create (this. prototype); 45 46 object. created (); 47 This. inherited (); 48 This. inherited (object); 49 50 return object; 51}, 52 // This function will initialize and return a new object 53 // It inherits from model. prototype, for example, an instance of the model object 54 init: function () {55 var instance = object. create (this. prototype); 56 instance. parent = This; 57 instance. init. apply (instance, argument S); 58 59 return instance; 60}, 61 // This method extends multiple attributes for the model object 62 extend: function (o) {63 var extented = O. extended; 64 65 $. extend (this, O); 66 If (extented) {67 extented (this); 68} 69}, 70 // This method is for model object extended instance attributes 71 include: function (o) {72 var encoded DED = O. included; 73 74 $. extend (this. prototype, O); 75 if (included) {76 included ded (this); 77} 78} 79}; 80 81 // we need to keep the record persistent, you can add a newly created instance to this object when saving it. 82 // When deleting an instance, delete it from the object 83 model. records ={}; 84 model. include ({85 newrecord: True, 86 // create a isntance 87 create: function () {88 this. newrecord = false; 89 This. parent. records [this. id] = This; 90}, 91 // delete a instance 92 destroy: function () {93 delete this. parent. records [this. id]; 94}, 95 // update a instance existed 96 update: function () {97 This. parent. records [this. id] = this; 98}, 99 // save a instance100 save: function () {101 this. newrecord? This. create (): This. update (); 102}, 103 init: function (ATTS) {104 /*... */105}, 106 load: function (attributes) {107 /*... */108} 109 110}); 111/** 112 * added ID support 113 */114 model. extend ({115 // search a instance by id116 find: function (ID) {117 return this. records [ID]; 118} 119 create: function () {120 If (! This. ID) {121 this. id = math. GUID (); 122} 123 this. newrecord = true; 124 this. parent. records [this. id] = This; 125} 126}); 127 128/** 129 * For test130 */131 var asset = model. create (); 132 var user = model. create (); 133 134 var user = user. init (); 135 136 var asset = asset. init (); 137 asset. name = "Same, same"; 138 asset. id = 1; 139 asset. save (); 140 141 var asset2 = asset. init (); 142 asset2.name = "but different"; 143 asset2.id = 2; 144 asset2.save (); 145 146 147 </SCRIPT>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.