a simple data store for emberjs
$ npm install ember-simple-data
var SD = require'ember-simple-data';
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 = SDstorecreatename: 'user'id_key: '_id';var comments = SDstorecreatename: 'comment'id_key: '_id';
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.
// USAGESDattributedata_source_key // data_source_key is optional here, it will default to your defined keySDhas_manydata_source_key store_nameSDbelongs_todata_source_key store_nameSDpropertyfunction depsusersdefinename: SDattributecomments: SDhas_many'comment_ids' 'comment';commentsdefinetext: SDattributepost: SDbelongs_to'user_id' 'user'TEXT: SDpropertyreturn thisget'text'toUpperCase;'text';
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.
commentsupdate_id: 1701text: 'I have a feeling.'user_id: 'troi'true;
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.
commentsupdate_id: 1_id: 2_id: 3242true true### 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
commentscontains123451235; // falsecommentscontains1701; // 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.
commentsfind22get'_id'; // 22commentsfind22get'text'; // 'Greetings from planet Earth.'commentsfind22get'user'; // The user object that this post belongs tocommentsfind22get'user'get'name'; // 'Riker'// and Ember has shortcuts to fetch attributes using dot notationcommentsfind22get'user.name'; // '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
usersdefinename: SDattributeset: SDattribute // Error: user.set is a reserved key, can not define with it.;
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
usersdefinename: SDattribute_set: SDattribute'set';usersload_id: 1name: 'Picard'set: 'Belongs to data set 1701.';// we have to use our defined key to get and set in the appusersfind1get'_set'; // 'Belongs to data set 1701.'usersfind1set'_set' 'Belongs to data set 1.';// json output is the same but we can tell it to use the data source keysusersto_jsonusersfind1_set; // 'Belongs to data set 1.'usersto_jsonusersfind1 use_source_keys:trueset; // '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.
usersload_id: 'troi'name: 'Troi'comments:_id: 1701text: 'This is a stupid comment.'user_id: 'troi';