7.2 Models--defining Models

Source: Internet
Author: User
Tags iso 8601

I. Overview

1. A model is a class that defines the properties and behavior of the data that you present to the user. Users hope that if they leave your application and return (or if they refresh the page) anything they see should be represented by a model.

2. Ensure that Ember-data.js is introduced after Ember.js:

<type= "Text/javascript"  src= "Ember.js"></ Script > <  type= "Text/javascript"  src= "Ember-data.js"></  script>

3. For each model in the application, create a DS. sub-class of Model:

App/models/person.js

default Csi Model.extend ();

4. After you have defined a model class, you can begin to find and create records for this type. When interacting with the store, you need to use model name to specify the type of the record. For example, the store's find () method expects a string as its first argument, telling it what type of record to look for:

Store.findrecord (' person ', 1);

5. The table below shows how the model's name maps the path to the model file:

Model Name Model Class
photo app/models/photo.js
admin-user-profile app/models/admin-user-profile.js

Second, defining attributes

1. You can specify what properties a Modle has by using ds.attr . App/models/person.js

default DS. Model.extend ({  firstName:DS.attr (),  lastName:DS.attr (),  birthday:DS.attr ()});

2. When converting a JSON returned from the server to a record, the property is used when serializing a record to the server when it is modified.

3. You can also use attributes like other properties, including as part of a computed property. Often, you will want to define a computed property that combines or transforms the original attribute.

App/models/person.js

default DS. Model.extend ({  firstName:DS.attr (),  lastName:DS.attr (),  fullName:Ember.computed (function  () {    returnthis this. Get (' LastName ');  })});

4. If you do not specify the type of the property, it will be any type provided by the server. You can determine that a property is usually coerced to a specific type by type or attr :

App/models/person.js

default DS. Model.extend ({  birthday:DS.attr (' Date ')});

5. The default adapter provides the string, number, Boolean, and date property types. The custom adapters may provide additional property types, and the new type can be registered as a transform. Please see documentation section on the REST Adapter.

6. Be aware that Ember data serializes and deserializes dates according to ISO 8601. Example:2014-05-27t12:54:01

8. Options

ds.attr uses an optional hash as the second parameter:

(1) defaultvalue: If not provided, pass a string or a called function to set the default value of the property.

(2) Example:

App/models/user.js

default DS. Model.extend ({      username:DS.attr (' string '),      email:DS.attr (' string '),      verified: Ds.attr (false}),      createdAt:DS.attr (' string ', {          return New Date ();})  });

Third, defining relationships

Ember data contains several built-in relationship types that help us define how your model is interconnected.

1. One-to-one

Use Ds.belongsto to declare one-to-two relationships between two models:

App/models/user.js

default DS. Model.extend ({  profile:DS.belongsTo (' profile ')});

App/models/profile.js

default DS. Model.extend ({  user:DS.belongsTo (' user ')});

2. One-to-many

Use the ds.belongsto and Ds.hasmany combinations to declare a one-to-many relationship between two models:

App/models/post.js

default DS. Model.extend ({  comments:DS.hasMany (' comment ')});

App/models/comment.js

default DS. Model.extend ({  post:DS.belongsTo (' Post ')});

3. Many-to-many

Use Ds.hasmany to declare a many-to-many relationship between two models:

App/models/post.js

default DS. Model.extend ({  tags:DS.hasMany (' tag ')});

App/models/tag.js

default DS. Model.extend ({  posts:DS.hasMany (' Post ')});

4. Explicit inverses (Explicit reverse)

(1) Ember data will do its best to discover a relationship that maps to another. In the One-to-many code above, for example, Ember Datak can find out that changing the comments relationship should reverse the update of the post relationship, because post is the only relationship to the model.

(2) However, there are times when you may have many Belongsto/hasmanyfor the same type. You can use the ds.hasmany inverse option to specify which of the related models is reversed:

App/models/comment.js

default DS. Model.extend ({  onePost:DS.belongsTo (' post '),  twoPost:DS.belongsTo (' post '),  RedPost:DS.belongsTo (' post '),  bluePost:DS.belongsTo (' post ')});

App/models/post.js

default DS. Model.extend ({  comments:DS.hasMany (' comment ', {    ' redpost '  })});

You can also specify an inverse on a belgongsto , which can work as you expect.

5. Reflexive relation (self-correlating relationship)

When you want to define a self-correlating relationship, you must explicitly define the other party and set the corresponding display to reverse if you do not need the other party, set to null.

App/models/folder.js

default DS. Model.extend ({  children:DS.hasMany (' folder ', {inverse: ' parent ' }),  Parent:DS.belongsTo ( ' folder ', {inverse: ' Children ' });

App/models/folder.js

default DS. Model.extend ({  parent:DS.belongsTo (null  })});

7.2 Models--defining Models

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.