Defining ember-data models

As discussed in the introduction, models in any MVC application define the business domain properties and behavior that needs to be consumed by the end user of your application. As we learned from the Injecting the model for your template section, in Chapter 4, Managing Application State Using Ember.js Routes that models, these could either be plain static JavaScript objects, or JSON data that is fetched from a remote server.

Model objects, defined by the ember-data library abstracts away the complexities involved with handling the communication with the server, serializing, as well as de-serializing of the response received from the server. In order to define your ember-data model object, you just need to extend the DS.Model object present in ember-data module as follows:

import DS from "ember-data";

export default DS.Model.extend({
  title: DS.attr('string'),
  isbn: DS.attr('string'),
  pages: DS.attr('number'),
  description: DS.attr('string'),
  authors: DS.hasMany('author',{ async: true }),
  publisher: DS.belongsTo('publisher',{ async: true }),
  reviews: DS.hasMany("review",{ async: true })
});

The book model is present at chapter6/example1/app/models/book.js

As you can see in the preceding code, we created a new file in app/models directory with the name book.js. The book object has a few differences from the objects we have seen so far.

The book object extends the DS.Model object, instead of Ember.Object. The DS.Model class inherits itself from the Ember.Object class; as a result, you can use all the features provided by Ember.Object, including computed properties and observers. The DS.Model class also adds in other additional capabilities, including serializing, deserializing a record, and saving it back to the server.

Model attributes in DS.Model objects are also handled differently, as the library needs to know which properties need to be serialized when sending them to the server, and which properties to de-serialize when receiving the response from the server. As a result, a DS.Model object can have both types of properties: the ones that need to be synced with the server, and also the ones that are very specific to how you want to present the data to the end user, and not sync it back to the server. Model attributes that have to be serialized and deserialized from the server are assigned a type DS.attr(). The DS.attr takes in an optional argument defining the type of attribute. The currently supported attribute types are string, number, boolean, and date. When nothing is passed as an argument to DS.attr, then the library tries to automatically identify the type received from the server, and deserializes it. You can also pass in any default value that you need to set on the attribute by passing an optional second argument to the DS.attr method, such as the following:

DS.attr('boolean', {defaultValue: false})
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.50.156