• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

DataStore-Introduction

This version was saved 14 years, 6 months ago View current version     Page history
Saved by Charles Jolley
on September 11, 2009 at 1:32:09 pm
 

Welcome to the SproutCore DataStore Programming Guide.  This guide will tell you everything you need to know about working with data in your SproutCore application including how to import data from your server, work with local storage, and how to model data in your application.

 

Who Should Read This Guide

 

You should read this guide if you plan to use the SproutCore DataStore API to store and manage data objects in your application.

 

Outline

 

  • About the DataStore - Overview of the major parts of the DataStore
  • Defining Your Model – All about creating SC.Record classes, modeling relationships, etc.
  • Using Fixtures - How to get going quickly with test data before you have a server
  • About Records - Details on how SC.Record's work and their states. 
  • Using Records - Details on working with the SC.Record API.
  • About Queries - Queries and finding multiple Records in the Store
  • Using Queries - Details on working with the SC.Query API.
  • NestedStores - How to buffer changes in your application
  • DataSources - how to write the code to connect to your server

 

---

OLDER DOCS (will be merged into above at some point

 

About the DataStore API

 

The Store is where you can find all of your dataHashes. Stores can be chained for editing purposes and committed back one chain level at a time all the way back to a persistent data source.

 

Every application you create should generally have its own store objects. Once you create the store, you will rarely need to work with the store directly except to retrieve records and collections.  

 

Internally, the store will keep track of changes to your JSON data hashes and manage syncing those changes with your data source.  A data source may be a server, local storage, or any other persistent code. 

 

Loading from the server

 

To load data from a server you will typically set up a data source which has methods that are invoked by the store, such as when a record is retrieved, committed or needs to be fetched.

 

Modeling Your Data with SC.Record

 

A Record is the core model class in SproutCore. It is analogous to NSManagedObject in Core Data and EOEnterpriseObject in the Enterprise Objects Framework (aka WebObjects), or ActiveRecord::Base in Rails.

 

Defining record types

 

To create a new model class, in your SproutCore workspace (command line), do:

     sc-gen model MyApp.MyModel

 

This will create MyApp.MyModel in clients/my_app/models/my_model.js. As you will notice, your model extends SC.Record and lets you set up its model attributes and foreign key relationships (if any). For instance, to define a title attribute in MyModel, you could do: 

 

title: SC.Record.attr(String, {
     isRequired: true,
     defaultValue: "No title"
}),

 

This will tell SC.Record that the title attribute is a String, and will only allow strings to be set (it will call toString() if they are not). The attribute type can be String, Date, Boolean, Number or even a custom defined type (see below).

 

In this case, we also decided to make sure that any model objects of this type require the title attribute and we will set default value to "No title" if passed value is null or undefined.

 

Defining object graphs 

 

As well as using the built in object types (such as String), you can also define your custom object type such as a different model type.

 

parentModel: SC.Record.attr(MyApp.ParentModel)

 

 

 

 

Outline

 

  1. About the DataStore API
    1. Working with Data in your application
      1. Loading from the server
      2. Saving Locally 
      3. Tracking Changes
    2. SC.Store
      1. Store chaining
      2. Using stores as editing contexts
    3. SC.LocalStore
    4. SC.Server and related
    5. SC.Record and related
      1. SC.SparseArray
      2. SC.SmartGroup
    6. Working with Large data sets
  2. Modeling Your Data with SC.Record
    1. Defining record types - mapping back to core JSON hash types
    2. Defining object graphs - hasMany, etc
    3. Lazy-loading data
      1. SC.SparseArray
      2. delayed loading records
    4. Auto-updating your data with SC.SmartGroup
  3. Using the SC.Store
    1. Adding/removing JSON attribute hashes
    2. Working with indexes
    3. Finding JSON hashes
    4. Using SC.Store as an editing context
  4. Saving and Loading Your Data
    1. Using SC.Server and related to fetch/save data
      1. Note using sc-config proxy...
    2. Serializing your data to a REST/JSON backend
    3. Working with an XML-based backend
    4. Working with arbitrary backends
  5. Storing Local data
    1. SC.UserDefaults
    2. SC.LocalStore

 

NOTE: This outline is very preliminary as most of the code this outline describes has not been written yet.  Also, SC.Collection will likely be deprecated in the future API and replaced with the more efficient SC.SparseArray and SC.SmartGroup classes, which is why it is not mentioned here.

Comments (0)

You don't have permission to comment on this page.