Table of Contents
Getting data in Model
Identification model relationship
Understanding the Ember-Data relational model
Understanding Ember-Data edge (sideloaded) loading
Customized Adapter
Customizing Serializer
Customized URL
Home Web Front-end HTML Tutorial In-depth understanding of Ember-Data features (Part 2)_html/css_WEB-ITnose

In-depth understanding of Ember-Data features (Part 2)_html/css_WEB-ITnose

Jun 24, 2016 am 11:43 AM

Written in front

I have been very busy recently. I have changed to a new job and I have to learn a lot of new technology stacks. I have also found a lot of excuses for myself not to keep blogging. It is often ironic that more of the remaining time is not used but wasted. Maybe this is youth, squander it. This is not what I want. In this case, I will continue to write, Keep doing a good job in blogging and strive to be among the top 100 blogs. Please keep this in mind.

                                                        Late at night on September 7th, I was next to the computer.

Article index

JS front-end framework Ember.js series

2. Use Ember-Data

In order to better use Ember-Data, you need to use Store, Store You can think of it as an in-memory cache that Ember-Data uses to restore and save data models. In fact, the Store is also responsible for obtaining data from the server through the bound Adapter.

You can also customize the Adapter:

Or customize the serializer:

Here are just Point out the custom scenario, which will be analyzed in detail next.

Getting data in Model

The following are two simple ways to get data:

  1. Store.find('mainMenu'); This method is trying to get All data of type mainMenu.
  2. Store.find(‘mainMenu’, ‘JSFlotJAgent’); This method is trying to obtain data of type mainMenu and id of type “JSFlotJAgent”.

For more examples, please refer to: http://emberjs.com/api/data/classes/DS.Store.html

Identification model relationship

The mainMenu and Children types in the above model are both mainMenu, forming the concept of an "infinite" tree with nested node objects in a loop. The following figure shows the relationship between mainMenu and Chart in the model:

Note: OneToOne, OneToMany, etc. in the above picture are ambiguous. The author's picture should be changed to OneToNone and ManyToNone.

Here mainMenu has a parent node to identify the upper-level reference relationship, a children node to represent the collection of child nodes, and finally a one-to-one chart to represent chart data. Chart can be understood as the main content of a node, and parent and children represent the location and relationship of the current node, which is somewhat similar to two pointers pointing to the parent node and child node respectively.

In the next section we analyze the model relationships in Ember-Data in detail.

3. Ember-Data model association

Ember-Data supports multiple data relationship types. These relationship types are used to expect the data type structure returned from the data end. Next, let’s analyze the role of these APIs in detail.

Understanding the Ember-Data relational model

Ember-Data defines 5 relational models, three of which are real types, and the other two can be understood as special examples .

Note: OneToNone in the second line of the picture is replaced with OneToOne. The author may have made a typo here.

Among them, as we mentioned in the previous section, there are many conventions in Ember-Data. For example, the defined attributes must be in Camel style (such as modelA), the list array attributes must be ended with s (such as modelAs), and all ids must be Uniqueness, which makes it easier to read the JSON Hash object passed from the server.

Understanding Ember-Data edge (sideloaded) loading

Edge loading is loading in steps by increasing the data level (can be understood as loading the first level of data collection first, and then loading as needed Load a sub-level data collection).

Let’s first look at the definition of Model:

This is a typical article and comment structure, that is, an article contains multiple comments, and one comment belongs to an article.

Let’s see how edge loading works:

When requesting all data of type, Ember-Data found that there is no such data in the warehouse. Then a request is made to the server, and the server only returns the article content and comment id (which can be understood as only returning a preview of the comment, not all the information of a comment). Then when the comment details are requested again, the server returns the real information of the comment.

You can have the server return all data when requesting data for the first time: (instead of waiting for the comment request to return)

Note: In this scenario, if the user does not view the comments, the comments will never be displayed, which means that the data may be loaded meaninglessly.

Of course, this also depends on your data scenario. Maybe this model is not suitable for you. Doing so will generate more HTTP requests, but at the same time it will reduce the amount of "available" data returned. How? The trade-off requires careful analysis, and of course you can also customize how to return data.

4. Customize Adapter and Serializer

Here Ember provides three basic scenarios for rewriting Adapter:

  1. There is no universal standard for the data type of data transmitted by the server API.
  2. The data sent from the server API is different from the data type you defined.
  3. Rewrite the Adapter and retain the Serializer to ensure that the type in the top-level structure of the Json data sent from the server is different from the type you defined, but the subtypes are the same.

Customized Adapter

Let’s take a look at this scenario:

In the data type in our previous section, mainMenu Contains a one-to-one Chart type. The current requirement is to set the Timespan of the Chart to partially obtain the Chart data. If it is not set, the default is 10 minutes.

First, extend a type from DS.RESTAdapter and follow the Adapter naming convention (XXXXAdapter must follow this pattern).

Then, you can rewrite the following method:

For our current needs, we only need to rewrite the find() method, which will Make an HTTP request.

Among them, the important thing is to use Timespan to reorganize the http request, which has achieved our purpose.

For more information, please refer to: http://guides.emberjs.com/v1.11.0/models/customizing-adapters/

Customizing Serializer

Compared to RESTSerializer , we give the following non-standard data model:

In this return type, user_name, account_name, user_role are non-standard data relative to RESTSerializer. And the top-level attribute name user_model does not match.

We can rewrite the methods in the Adapter to achieve our goals:

Here we need to rewrite the normalize method in order to solve the problem of non-standardization of user_name, account_name, user_role, Override the typeForRoot method to solve the user_model problem.

Customized URL

You can customize the ULR access type and conversion definition of the Adapter. Here is a good example:

The final access will be as follows URL: http://api.myapp.com/json/v1/mainMenus

FAQ
  1. Can the advantages of using Store to store data be combined with Filter?

Store can automatically cache data and maintain data identity through the id attribute. And allows users to use Filter to filter unwanted data.

  1. How does Ember-Data notify that new data has arrived?

When you use store.find(), the data will automatically update the RecordArray, and any calculated properties or listeners on the RecordArray will be notified. If you do not need to request and want to load data, you can use Store.push or pushPayload to push data to the Store, so that the Store will also get the listening event triggered. Such as SSE or WebSocket scenarios.

Quote

Ember-Data update log: https://github.com/emberjs/data/blob/master/TRANSITION.md

Bibliography: "Ember.js In Action"

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles