ember-cli-simple-store
Description
ember-cli addon that provides a simple identity map for ember.js web applications
Installation
ember install ember-cli-simple-store
You get 6 methods: push/pushArray/remove/find/findOne/clear
//create or update person model simpleStore;
//create or update multiple person models - observers are notified only once all models have been created or updated simpleStore;
//remove person model with id=123 simpleStore;
//find all person models simpleStore;
//find a single person model with id=123 simpleStore;
//find all person models with account_id=789 simpleStore;
//find all person models with name toran and salary > 100 var { var name = person; var salary = person; return name === "toran" && salary > 100;}simpleStore;
//find the first person model simpleStore;
//clear the entire identity map of all person models simpleStoreclear"person";
//clear the entire identity map of all models simpleStoreclear;
Using the store by example
Below I'll show how you can use the store with a simple ember object to find/add/remove/update
The full example below relies on a small xhr addon PromiseMixin
;; var PersonRepository = EmberObject; ;
What about relationship support?
A one-to-many example might look something like this
; Object; //a test to show how this api works from the outside ;
What about dirty tracking?
If you want the ability to track if your model is dirty use the attr for each field and the Model to get save/rollback
; var Person = Model; //save your object to reset isDirtyvar person = Person;person;person; //rollback your object to reset isDirty and restore itperson;person;
If you want to know if an individual property isDirty you can ask like so
person; //undefinedperson;person; //true
For new forms that start with undefined properties you can define the default state for isDirty. Example: you have a model that is bound to a form with a checkbox input. The create form starts with a new model so each bound property is undefined. If the user decides to check the bound checkbox (setting the value to true) and then decides to uncheck it (setting the value to false) you would expect the form is not dirty - but because undefined !== false you find the model is dirty. To prevent this behavior set a default value for dirty tracking on the models attr like so.
var Animal = Model; var animal = Animal; animal; //undefinedanimal; //falseanimal;animal; //trueanimal;animal; //false
What about custom primary keys?
By default, simple store will use the 'id' property on your records as the primary key. If you wish to override this, reopen your
model class and add a primaryKey
attribute.
var Food = EmberObject; Food; simpleStore; // Returns the pushed recordsimpleStore;
Example applications
Simplest example with the least amount of complexity (tests included)
https://github.com/toranb/kanban-board-without-ember-data
Simple example running ember 2.4
https://github.com/toranb/ember-2-skeleton-app
Async example that will paint right away (loading section included w/ tests)
https://github.com/toranb/async-kanban-board-store-example
Async example with relationships (loading section included w/ tests)
https://github.com/toranb/async-kanban-with-relationships-store-example
Dirty tracking example with both save and rollback
https://github.com/toranb/ember-cli-store-dirty-tracking-example
Running the unit tests
npm install
ember test
License
Copyright © 2015 Toran Billups http://toranbillups.com
Licensed under the MIT License