Ember.js Getting Started Guide--model introduction 2

Source: Internet
Author: User

This article is followed by a "ember.js Getting Started Guide--model introduction 1".

2, the core concept

Statement : The following brief excerpt to http://www.emberjs.cn/guides/models/#toc_.

1,Store

Store is the central repository where records are applied. You can think of store as the cache of all the data that is applied. The app's controllers and routes can access the shared store, and when they need to display or modify a record, they first need to access the store.

Ds. an instance of the store is created automatically, and the instance is shared by all objects in the app.  

the store can be seen as a cache. The following cache will be introduced in conjunction with the store .

The following example combines Firebase Demo:

To create a route and model:

Ember G Route Store-exampleember G model article
App/models/article.js import DS from ' Ember-data '; Export default DS. Model.extend ({title:DS.attr (' string '), Body:DS.attr (' string '), timestamp:DS.attr (' num ber '), category:DS.attr (' string ')});

This is the model, which is what this chapter is about! Why is the ID attribute not defined? Ember will generate the ID attribute by default.

we get the remote data in the model callback of the route and display it on the template.

App/routes/store-example.js import Ember from ' Ember '; Export Default Ember.Route.extend ({model:function ()}//Get ID JZYSRMBIVASSFG6WW from store            OK data, this data is I initialize in my firebase good return this.store.find (' article ', '-jzysrmbivassfg6wwok '); }});

the first parameter of the Find method is the model class name, and the ID property value of the second parameter object . Remember that the ID attribute does not need to be defined manually in the model class, andEmber is automatically defined for you.

After the page loads, you can see the data that you obtained.

here is some of the data on my firebase.

you can see the successful acquisition of data with ID-jzysrmbivassfg6wwok. More information about the data is described later in this article.

2,Model

The previous introduction to the model concept has been introduced and is not covered here.

Model Definition:

Model is composed of several attributes. The parameters of the Attr method specify the type of the property.

Export default DS. Model.extend ({title:DS.attr (' string '),//String Type Flag:DS.attr (' Boolean '),//Boolean type Ti Mestamp:DS.attr (' number '),//numeric type birth:DS.attr (' date ')//Date type});

The model also declares its relationship to other objects. For example, an order can have many LineItems, and a LineItem can belong to a specific order.

App.order = DS. Model.extend ({lineItems:DS.hasMany (' LineItem ')}); App.lineitem = DS. Model.extend ({order:DS.belongsTo (' order ')});

This is the same relationship as the table of data.

3,Record

The record is an example of a model that contains data that is loaded from the server side. The app itself can also create new records and save new records to the server side.

Records are uniquely identified by the following two properties:

1, model type

2, a globally unique ID

For example, the previous instance of article is obtained through the find side. The result obtained is a record.

4,Adapter

an adapter is an object that understands the backend of a particular server and is primarily responsible for recording (record) requests and changes are converted to the correct call to the server side of the request.

For example, if an application requires a The person record with ID 1 , then how does Ember Data load this object? Is it through HTTP, or Websocket? If it is through HTTP, then the URL will be/person/1, or /resources/people/1 it?

the adapter is responsible for handling all similar problems. Whenever an app needs to get a record from the store that is not cached, the app accesses the adapter to get the record. If a record is changed and a change is ready to be saved,the store passes the record to the adapter, which is then the adapter responsible for sending the data to the server side and confirming that the save was successful.

5,Cache

Store will automatically cache records. If a record is already loaded, the same object instance is returned when it is accessed again. This greatly reduces the round-trip communication with the server side, allowing the application to render the desired UI to the user faster .

For example, when an app first obtains a person record with ID 1 from the store , the object's data is fetched from the server side.

However, when the application needs again When the person record with ID 1 is logged,the store will find that the record has been acquired and the record has been cached. instead of sending a request to the server to fetch the recorded data, the store directly returns the record that was acquired and constructed at the first time. This feature makes it possible to return the same record object regardless of how many times the record is requested, which is also known as Identity map(identifier mapping).

using identifier mapping is important because it ensures that a changes to one record on the UI are automatically propagated to the UI other UI that uses that record . At the same time this means that you do not have to manually keep the object in sync, just use the ID to get the record that the app has acquired.

3, Introduction to Architecture

When the app gets a recordfrom the store for the first time , the store discovers that the local cache does not have a copy of the requested record, and then sends a request to the adapter. The adapter will fetch records from the persistence layer, and typically, the persistence layer is an HTTP service through which a JSON representation of the record can be obtained .

as shown, the adapter sometimes cannot return the requested record immediately. The adapter must then initiate an asynchronous request to the server before it can be created by the returned data after the request has completed loading.

because of such asynchrony,store find() Returns a promise (Promise) immediately from the method. In addition, all requests that require the store to interact with the adapter will return a commitment.

once a request to the server is sent back to the requested record JSON data, the adapter fulfills the promise and passes the JSON to the store.

The store then acquires the JSON anduses the JSON data to complete the initialization of the records and uses the newly loaded records to fulfill the commitments that have been returned to the application.

here 's a look at what happens when the store has cached the requested records.

in this case,the store has cached the requested record, but it will also return a promise, except that the promise will immediately be fulfilled with the cached record. At this point, since the store already has a copy, there is no need to request the adapter (no interaction with the server).

Models,Records,adapters, andstore are the concepts you have to understand. This is the core thing of Ember Data.

There are concepts about the above that will be shown in the following article one by one in code. Understand this article model This whole chapter of the content is not a problem!!!


Ember.js Getting Started Guide--model introduction 2

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.