Using Dojo's AJAX applications to develop advanced tutorials, part 7th: An in-depth introduction to the Dojo Core library

Source: Internet
Author: User
Tags arrays json connect requires

The Dojo Core Library, built on the dojo base, provides richer functionality for the development of AJAX applications. Mastering the content contained in the Dojo Core library allows developers to reduce the amount of code and focus on the implementation of components related to business logic. The Dojo core library contains more content, and this article is just a few of the more important or complex parts. First, start with the data model.

Data model

In traditional WEB applications, the client part has little responsibility and is basically responsible for data presentation. The processing of the complex data model involved in the application is done by the server-side code. In Ajax applications, the client part also needs to handle part of the business logic, including data model processing. The presence of complex user interface components, such as tables and trees, also requires clients to handle complex data models in the background of components. A typical Ajax application uses a complex JSON object as its client's data model, and the application itself maintains the model. The problem is that the cost of maintenance is too high for the data to be shared easily between different models. The Dojo.data module is provided in the Dojo core library to solve this problem. It defines a number of standard interfaces for data access and manipulation, as well as related implementations. Data consumers use the same interface to access and manipulate data, increasing interoperability between them. A unified interface also makes code portability for developers better. Developers also do not need to learn and understand a lot of proprietary data access interfaces and protocols.

Dojo.data the abstraction of the data is very simple. The data store, which is responsible for managing the data, is a collection of entries (item). And the entry is a collection of attributes (attribute). This abstraction is conceptually similar to a table in a relational database. A table in a relational database is a collection of rows, and a row is a collection of fields. The key difference is that the rows of the tables in the relational database are isomorphic and contain the same fields; the entries in Dojo.data can be heterogeneous and contain completely different properties. The Dojo.data API is spread around this abstraction. It contains 4 types of APIs, namely read (Dojo.data.api.Read), write (Dojo.data.api.Write), Identifiers (Dojo.data.api.Identity) and Notifications (dojo.data.api.Notification). The read and write APIs are used to read and update entries in the Data Warehouse, respectively. The identifier API is used to provide a unique identifier for each entry in the Data Warehouse, and it is possible to obtain entries from this identifier. The notification API is used to be notified when entries in the Data Warehouse change, including the creation, deletion, and updating of attributes.

In terms of space, this article does not detail the full API of Dojo.data, but discusses some of the areas and best practices that need to be noted. The fetch () method in the Dojo.data.api.Read interface is executed asynchronously and requires an incoming callback method. Even though entries in the Data warehouse can be obtained locally, the callback method is also required. This is necessary from an API point of view, but it can cause some coding headaches. Through with dojo. Deferred together, you can simplify the writing of code.

The Dojo.data.api.Notification API is useful when dojo.data data warehouses as data sources in the background of user interface components. It can be used to implement a typical observer design pattern. When the data in the Data warehouse changes, the user interface components also need to be updated accordingly. By using Dojo.connect () to connect to the OnNew (), OnDelete (), and onset () methods of the Data warehouse, you can be notified when creating entries, deleting entries, and updating item properties. It should be noted here that when an entry is deleted, the object is available in the OnDelete () method, but the entry is no longer part of the original data warehouse, and can not be getValue () and other methods to get the properties of the entry. Onset () is invoked when each property is updated. If you update multiple properties of an entry at the same time, you need to be aware of the repeated invocation of the onset () method.

The Dojo Core Library provides two implementations of the data warehouse based on JavaScript objects Dojo.data.ItemFileReadStore and Dojo.data.ItemFileWriteStore. These two warehouses can serve as a bridge between the data returned from the server side and the Dojo user interface components. Many dojo user interface components, such as tables, trees, and Drop-down lists, use the Dojo Data Warehouse as their background data model. It is common practice to create a dojo.data.ItemFileReadStore from server-side data that is available to these user interface components. Dojo.data.ItemFileWriteStore can also be a good substitute for JavaScript arrays. An array is often used in Ajax applications to maintain internal domain objects. Using arrays is not easy to find, update, and delete objects, and you typically need to add extra code. Using Dojo.data.ItemFileWriteStore can be a good solution to this problem. It provides support for the notification API and can also help write more concise code to handle object changes. When using these two data warehouses, you need to be aware of how the attributes of an array type in a JavaScript object are accessed. Multiple elements contained within an array need to be read by means of the GetValues () method. If you use the GetValue () method, you can read only the first element of the array. If you want to be able to unify the GetValue () method, you need to convert the array to a new array of only one element before it is stored in the Data warehouse. An example of how entries in the Data Warehouse and JavaScript objects are converted to each other is given in Listing 1.

Listing 1. Converting between entries and JavaScript objects in the Data Warehouse

var item = {
   name : "Alex",
   skills : ["JavaScript", "Java"]
  };

  function addJsonToStore(store, json) {
   for (var key in json) {
     if (json.hasOwnProperty(key) && dojo.isArray(json[key]) {
       json[key] = [json[key]];
     }
   }
   store.newItem(json);
  }

  function storeItemToJson(store, item) {
   var attrs = store.getAttributes(item);
   var result = {};
   dojo.forEach(attrs, function(attr) {
     result[attr] = store.getValue(item, attr);
   });
   return result;
  }

As shown in Listing 1, the Addjsontostore () method handles arrays in JavaScript objects. When you convert an entry in a data warehouse into a JavaScript object by using the Storeitemtojson () method, you can use the GetValue () method directly without errors.

After introducing the data model provided by the Dojo Core Library, the following describes the content related to I/O requests.

Related Article

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.