ember-cli-simple-store
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -40,3 +40,3 @@ import Ember from "ember"; | ||
push: function(type, data) { | ||
var record = this._getById(type, data.id); | ||
var record = this._findById(type, data.id); | ||
if (record) { | ||
@@ -50,3 +50,3 @@ record.setProperties(data); | ||
remove: function(type, id) { | ||
var record = this._getById(type, id); | ||
var record = this._findById(type, id); | ||
if (record) { | ||
@@ -59,3 +59,3 @@ delete this.get("identityMap")[type][record.id]; | ||
if (typeof options === "undefined") { | ||
return this._getEverything(type); | ||
return this._findAll(type); | ||
} | ||
@@ -69,14 +69,18 @@ if (typeof options === "object") { | ||
var value = options[attr]; | ||
return this._filterEverything(type, attr, value); | ||
return this._findWithFilter(type, attr, value); | ||
} | ||
return this._getById(type, options); | ||
return this._findById(type, options); | ||
}, | ||
_getById: function(type, id) { | ||
findOne: function(type) { | ||
var all = this._findAll(type); | ||
return all.length > 0 ? all.objectAt(0) : null; | ||
}, | ||
_findById: function(type, id) { | ||
var identityMap = identityMapForType(type, this); | ||
return identityMap[id] || null; | ||
}, | ||
_getEverything: function(type) { | ||
_findAll: function(type) { | ||
return arrayForType(type, this); | ||
}, | ||
_filterEverything: function(type, filter_attr, filter_value) { | ||
_findWithFilter: function(type, filter_attr, filter_value) { | ||
var computed_string = "source.@each." + filter_attr; | ||
@@ -91,3 +95,3 @@ return Ember.ArrayProxy.extend({ | ||
filter_value: filter_value, | ||
source: this._getEverything(type) | ||
source: this._findAll(type) | ||
}); | ||
@@ -94,0 +98,0 @@ } |
{ | ||
"name": "ember-cli-simple-store", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "ember-cli addon that provides a simple identity map for ember.js web applications", | ||
@@ -5,0 +5,0 @@ "directories": { |
# ember-cli-simple-store | ||
[![Build Status][]](https://travis-ci.org/toranb/ember-cli-simple-store) | ||
[![NPM Downlaads](https://img.shields.io/npm/dm/ember-cli-simple-store)](https://www.npmjs.org/package/ember-cli-simple-store) | ||
## Description | ||
@@ -11,14 +9,16 @@ | ||
## Installation | ||
``` | ||
$ npm install ember-cli-simple-store --save-dev | ||
1) remove ember-data from your package.json file | ||
2) remove ember-data from your bower.json file | ||
3) rm -rf node_modules/ember-data | ||
4) rm -rf bower_components/ember-data | ||
5) npm install ember-cli-simple-store --save-dev | ||
``` | ||
## You get 5 methods: push/remove/find/findOne/clear | ||
## The entire api => create/find/delete/clear/update | ||
```js | ||
//create a new person model | ||
//create or update person model | ||
@@ -29,8 +29,2 @@ this.store.push("person", {id: 1, name: "toran"}); | ||
```js | ||
//update an existing person model | ||
this.store.push("person", {id: 1, name: "brandon"}); | ||
``` | ||
```js | ||
//remove person model with id=123 | ||
@@ -60,8 +54,13 @@ | ||
```js | ||
//clear the entire identity map of all person model objects | ||
//find the first person model | ||
this.store.findOne("person"); | ||
``` | ||
```js | ||
//clear the entire identity map of all person models | ||
this.store.clear("person"); | ||
``` | ||
## Using the store by example | ||
@@ -74,19 +73,19 @@ | ||
```js | ||
import PromiseMixin from 'js/mixins/promise'; | ||
import PromiseMixin from "js/mixins/promise"; | ||
var Person = Ember.Object.extend({ | ||
firstName: '', | ||
lastName: '', | ||
phone: '' | ||
}).reopenClass(PromiseMixin, { | ||
firstName: "", | ||
lastName: "", | ||
phone: "" | ||
}).reopenClass({ | ||
find: function(store) { | ||
return this.xhr('/api/people/', 'GET').then(function(response) { | ||
return PromiseMixin.xhr("/api/people/", "GET").then(function(response) { | ||
response.forEach(function(person) { | ||
store.push('person', person); | ||
store.push("person", person); | ||
}); | ||
return store.find('person'); | ||
return store.find("person"); | ||
}); | ||
}, | ||
findById: function(store, id) { | ||
return store.find('person', id); | ||
return store.find("person", id); | ||
}, | ||
@@ -97,4 +96,4 @@ insert: function(store, person) { | ||
return new Ember.RSVP.Promise(function(resolve,reject) { | ||
return self.xhr("/api/people/", "POST", hash).then(function(persisted) { | ||
var inserted = store.push('person', Person.create(persisted)); | ||
return PromiseMixin.xhr("/api/people/", "POST", hash).then(function(persisted) { | ||
var inserted = store.push("person", persisted); | ||
resolve(inserted); | ||
@@ -110,3 +109,3 @@ }, function(err) { | ||
var endpoint = "/api/people/%@/".fmt(person_id); | ||
return this.xhr(endpoint, "PUT", hash); | ||
return PromiseMixin.xhr(endpoint, "PUT", hash); | ||
}, | ||
@@ -118,4 +117,4 @@ remove: function(store, person) { | ||
return new Ember.RSVP.Promise(function(resolve,reject) { | ||
return self.xhr(endpoint, "DELETE").then(function(arg) { | ||
store.remove('person', person_id); | ||
return PromiseMixin.xhr(endpoint, "DELETE").then(function(arg) { | ||
store.remove("person", person_id); | ||
resolve(arg); | ||
@@ -132,3 +131,2 @@ }, function(err) { | ||
## What about relationship support? | ||
@@ -156,5 +154,10 @@ | ||
This approach is not without it's tradeoffs (ie- additional http calls to fetch related data instead of using embedded json for example). I've personally found this is a great approach for apps that want to avoid the "kitchen-sink" problem. | ||
This approach is not without it's tradeoffs | ||
* additional http calls to fetch related data instead of using embedded json. You could make a single http call and parse this out if latency becomes problematic but you might find yourself managing complex object hierarchies all over again. | ||
* you will find yourself passing the store instance into model object class methods from the route/controller | ||
* you begin to use a different pattern for object materialization/filtering in the route objects because the models themselves are relationship-less. | ||
I've personally found this is a great approach for apps that want to avoid the complexity of bigger projects like ember-data, but still need a single pointer /reference for the models in your ember application. | ||
## What about the missing MyObject.save() abstraction | ||
@@ -164,8 +167,7 @@ | ||
## Running the unit tests | ||
npm install | ||
ember test | ||
## Example project | ||
@@ -175,3 +177,2 @@ | ||
## License | ||
@@ -184,5 +185,5 @@ | ||
[Build Status]: https://secure.travis-ci.org/toranb/ember-cli-simple-store?branch=master | ||
[Build Status]: https://travis-ci.org/toranb/ember-cli-simple-store.svg?branch=master | ||
[ember-cli]: http://www.ember-cli.com/ | ||
[ember.js]: http://emberjs.com/ | ||
[PromiseMixin]: https://gist.github.com/toranb/98abc9616f2abecde0d4 |
@@ -345,1 +345,28 @@ import Ember from "ember"; | ||
}); | ||
test("findOne will return the first record", function() { | ||
var first = store.push("person", { | ||
id: 1, | ||
firstName: "Toran", | ||
lastName: "Billups" | ||
}); | ||
var last = store.push("person", { | ||
id: 2, | ||
firstName: "Brandon", | ||
lastName: "Williams" | ||
}); | ||
equal(store.find("person").length, 2); | ||
var toranb = store.findOne("person"); | ||
equal(toranb.get("firstName"), "Toran", "the firstName property is correct"); | ||
equal(toranb.get("lastName"), "Billups", "the lastName property is correct"); | ||
equal(toranb.get("id"), "1", "the id property is correct"); | ||
}); | ||
test("findOne should return null when no objects exist in the cache for given type", function() { | ||
equal(store.find("person").length, 0); | ||
var person = store.findOne("person"); | ||
deepEqual(person, null); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30525
546
178