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:
Models form the core of any MVC design pattern as they describe the core business
domain at hand. In Ember.js, every route has an associated model that describes the
business domain that the route needs to work on. Almost all of the examples that we
have seen so far operate on the static data that is returned from the model property
of the routes. But most of the times the data that needs to be operated upon is not
static and is fetched from a remote server.
Traditionally we have used jQuery or plain JavaScript to fetch the data from the
server, using AJAX calls and then using those returned JSON objects as models
in our application. This approach works well for applications that are simpler in
nature, but very quickly get difficult to maintain when working on domains that are
complex in nature, especially the ones that have relationships among them. We need
to make sure that the correct relationship is linked and updated in our application.
The ember-data library tries to solve the problems highlighted earlier that are
encountered in building single page web applications, by providing a standard way
of accessing, finding, modifying, and saving the model objects of your application.
By using the ember-data library, you can easily simplify your code that handles
the communication with the backend server, by letting ember-data handle most of
the complexity for you. This lets you focus more on solving the business problem at
hand. The library also brings in a lot of optimizations to your code that improve the
overall performance of your application.
Ember Data is designed to be agnostic to the underlying storage technique, and hence
it works equally well will HTTP based JSON API, as well as streaming WebSockets.
The ember-data library is actively being developed and improved, and is under the
beta quality. The API is highly stabilized and there are a lot of companies who have
been using ember-data in production for quite some time now.
The current beta of ember-data is already included with Ember CLI and can be
checked from the bower.json file, present in the project’s root directory.
Defining ember-data models
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:
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.
Comments
Post a Comment