Chapter 4. The Model Layer

In this chapter, we will move to the opposite end of the application stack and look at the M or Model layer in SproutCore MVC. As it turns out, the implementation of the Model layer within SproutCore is one of the most impressive and unique features of the framework. This layer provides tools for modeling data, applying business logic, querying data, and synchronizing changes between the server and the client. Hence, we can say that the tools in this layer allow us to load raw data into our application from any remote source and use it in a simple and effective manner.

The first thing to recognize is that because we are creating applications for the web, our software is always in a client-server type of relationship where the actual data is stored separately from the application. Even when using a client-side data store such as IndexedDB or WebSQL, our application must asynchronously interface with the data store the same way it would as though the database were on a remote server somewhere. To fulfill the need of interfacing with a variety of remote sources, SproutCore defines a standard Application Programming Interface (API) that we can use to write custom data source adaptors. These adaptors belong to, what I called the Data Interface layer in the previous chapter. We will look at the data source adaptor API in this chapter as well so that you fully understand how to integrate with your own remote data sources.

In this chapter, we will cover the following:

  • Understanding the Model and Data Interface layers
  • Introducing the data store
  • Defining your data model
  • Creating custom attribute properties
  • Defining record relationships
  • Following the life cycle of SC.Record
  • Querying the store
  • Synchronizing data with a remote data source
  • Implementing remote data source adaptors

Understanding the Model and Data Interface layers

As I alluded to in the very first chapter of this book, unavoidable network latency prevents any traditional server-side web application from nearing the performance of a native application. When a web application embeds data into templates on the server and serves them one at a time to the user, the experience will be, at best, okay on the desktop, and, at worst, unusable on mobile.

However, while network latency is unavoidable and worsens as users move away from desktop to mobile, we will still provide an exceptional user experience in web applications that use SproutCore. We saw that SproutCore's View layer takes care of most of this problem by allowing us to run a responsive and feature-rich UI in the client, but it would be all for naught, if we didn't have the means to supply that UI with data.

Thus, this is the role of the Model layer: to acquire, store, and provide data for our application's use. To acquire data, the Model layer interacts with the Data Interface layer to request information from a remote source; to store data, the Model layer places the information in one or more local data stores; and to provide data, the Model layer includes a powerful query language and a complete data modeling structure.

Note

Remember that while not all the data will be ready immediately, we can usually make this unnoticeable to the user by not blocking the UI while data is being synchronized and also by pre-fetching data behind the scenes whenever possible. As an added bonus, since we only need raw data from the server and not entire views, each request and response is as fast as possible.

..................Content has been hidden....................

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