Skip to main content

Introducing ember-data

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

Popular posts from this blog

It was a great experience to talk to a huge audience in Mumbai and Delhi about how to start your ML journey at Google Cloud Summit ’18 India

Sunset

Sunset

COVID-19 Lockdown

It is May 11th 2020 and most of India has been under lockdown for 46 straight days. These have been tough times for everyone. While some of the folks i know have been cribbing about being bored to death in this lockdown, for me it has been a busy time. My current field of work which is "Cloud Technology" picked up as everyone was locked up with nothing but technology to entertain and survive and hence there was a lot of work. While me and my family was trying to navigate the chaos and madness of ordering everything online, i would say we were able to deal with the situation without going out much.  This was until my phone broke, crashed, bricked, it was gone.  While i had a spare 4 year old iPhone to fall back on but you know about spare phones, they were too old to trade for a newer one is why you decided to label it as a spare and keep it. They almost never work. So after struggling to get my office and personal stuff to work on an old phone and tablet, i decided i needed t