a simple data store for emberjs
$ npm install ember-simple-data
var SD = ;
First we need to create some store objects that will house our models. Each store needs a unique name which we will use to reference it. The id keys on your models default to 'id' but you can specify which key to use instead, obviously it must be unique in your data set.
var users = SDstore;var comments = SDstore;
Then we need to define the models that will be inserted into the store. This is a description of the attributes on each object and its relationship to other objects you have. The
belongs_to attribute definitions require you pass the name of the store which these related objects are defined on and which attribute contains the list of ids or id.
// USAGESD // data_source_key is optional here, it will default to your defined keySDSDSD;comments;
This is an Ember Computer Property. Defined just like you would with Ember Objects, the deps argument is an array which contains the arguments you would normally pass to .property() on Ember Objects.
The json for our user objects would look something like this, where
comment_data would be a result of api calls for example.
var user_data =_id: 'riker'name: 'Riker'comment_ids: 22 123_id: 'troi'name: 'Troi'comment_ids: 1701;var comment_data =_id: 22text: 'Greetings from planet Earth.'user_id: 'riker'_id: 123text: 'Where are you Imzadi?'user_id: 'riker'_id: 1701text: 'This is a stupid comment.'user_id: 'troi';
And then we load it into our store by simply doing
We can update existing comment objects by using
comments.update() and passing it the object to update. It will do a find for the object id given and then update attributes. If we want to do an 'update or insert' then we can pass true as a second parameter to the update call and it will insert a new object if not found for updating.
We can also tell upate to clobber the data store by passing true as the third argument. Meaning that it will update existing objects and remove any from the store that weren't provided. This is basically like clearing the store and loading but you won't get display issues in ember where everything disappears for a split second.
comments### Finding objects```jscomments.find(); // get all currently in storecomments.find(1701); // specific object if it exists
If you just want to know if an id exists in your store you can use
comments; // falsecomments; // true
The objects are loaded into the store as instances of Ember.Object so you will need to use
.get() so that all the observer functions fire properly, especially with the relationship attributes.
comments; // 22comments; // 'Greetings from planet Earth.'comments; // The user object that this post belongs tocomments; // 'Riker'// and Ember has shortcuts to fetch attributes using dot notationcomments; // 'Riker'
Because we use Ember.Object for our records there are a number of reserved keys for Ember methods and such. If you try and use one as an attribute it will throw an error
Since we shouldn't have to change the source output data in order to be able to use it we can set a different key to find the data on like so
users;users;// we have to use our defined key to get and set in the appusers; // 'Belongs to data set 1701.'users;// json output is the same but we can tell it to use the data source keysusers_set; // 'Belongs to data set 1.'usersset; // 'Belongs to data set 1.'
The difference here is that instead of an array of
comment_ids it's an array of objects on the
comments attribute. This will load the comments into the comment store and then add the ids to the
comment_ids array on the user object. You must at this point in time also provide the relationships inside the comment object as though you were loading the comments in a non-embedded fashion.