Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-cli-simple-store

Package Overview
Dependencies
Maintainers
3
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-cli-simple-store - npm Package Compare versions

Comparing version 4.0.0-beta.6 to 4.0.0-beta.7

addon/models/filtered-record-array.js

58

addon/model.js
import Ember from "ember";
const { computed, defineProperty, get } = Ember;
function equal(first, second) {

@@ -7,2 +9,3 @@ if (first instanceof Array && second instanceof Array) {

}
return first === second;

@@ -22,2 +25,3 @@ }

});
return all;

@@ -33,2 +37,3 @@ }

});
return copy;

@@ -38,6 +43,11 @@ }

var attr = function() {
var meta = {isAttribute: true, defaults: arguments[0]};
return Ember.computed({
var meta = {
isAttribute: true,
defaults: arguments[0]
};
return computed({
get(key) {
var data = this.get("_data") || {};
return data[key];

@@ -65,2 +75,3 @@ },

}
return data[key];

@@ -72,4 +83,4 @@ }

var Model = Ember.Object.extend({
init: function() {
this._super();
init() {
this._super(...arguments);
this._reset();

@@ -81,3 +92,3 @@ this._setup();

},
rollback: function() {
rollback() {
var oldState = this.get("_oldState");

@@ -89,3 +100,3 @@ for(var key in oldState){

},
save: function() {
save() {
var oldState = clone(this);

@@ -95,3 +106,3 @@ this.set("_oldState", oldState);

},
_reset: function() {
_reset() {
this.set("isPrimed", false);

@@ -101,8 +112,8 @@ this.set("_dirty", {});

},
_setup: function() {
var self = this;
_setup() {
var model = this;
var attributes = attrs(this);
attributes.forEach(function(attrName) {
var dynamicDirtyKey = attrName + "IsDirty";
Ember.defineProperty(self, dynamicDirtyKey, Ember.computed(function() {
attributes.forEach((attrName) => {
defineProperty(model, attrName + "IsDirty", computed(function() {
var current = this.get(attrName);

@@ -114,20 +125,23 @@ var defaults = this.get("_defaults")[attrName];

var legit = (equal(current, defaults) && Ember.isNone(original)) || (equal(original, current));
return legit ? undefined : dirty[dirtyKey];
}).property("_dirty", "_defaults", "" + attrName));
var dynamicPrimedKey = attrName + "IsPrimed";
Ember.defineProperty(self, dynamicPrimedKey, Ember.computed(function() {
defineProperty(model, dynamicPrimedKey, computed(function() {
var primed = this.get("_primed");
var primedKey = attrName + ":isPrimed";
return primed[primedKey];
}).property("_primed", "" + attrName));
});
var modelIsDirtyAttrs = [];
attributes.forEach(function(attr) {
modelIsDirtyAttrs.push(attr + "IsDirty");
});
Ember.defineProperty(this, "isNotDirty", Ember.computed.not('isDirty'));
Ember.defineProperty(this, "isDirty", Ember.computed(function() {
var modelAttrs = modelIsDirtyAttrs.filter(function(attr){
return self.get(attr) === true;
});
var modelIsDirtyAttrs = attributes.map((attr) => attr + "IsDirty");
defineProperty(model, "isNotDirty", computed.not('isDirty'));
defineProperty(model, "isDirty", computed(function() {
var modelAttrs = modelIsDirtyAttrs.filter((attr) => get(model, attr) === true);
return modelAttrs.length > 0;

@@ -134,0 +148,0 @@ }).property("" + modelIsDirtyAttrs));

import Ember from "ember";
import getOwner from "ember-getowner-polyfill";
import RecordProxy from './models/record-proxy';
import RecordArray from './models/record-array';
import FilteredRecordArray from './models/filtered-record-array';
const { run, get, setProperties, assert } = Ember;
function buildRecord(type, data, store) {

@@ -8,6 +13,6 @@ var containerKey = "model:" + type;

Ember.assert("No model was found for type: " + type, factory);
assert("No model was found for type: " + type, factory);
var record = factory.create(data);
var id = data.id;
var { id } = data;
identityMapForType(type, store)[id] = record;

@@ -30,24 +35,29 @@ arrayForType(type, store).pushObject(record);

typeIdentityMap[type] = idIdentityMap;
return idIdentityMap;
}
var Store = Ember.Object.extend({
init: function() {
this.set("recompute", []);
this.set("filtersMap", {});
this.set("identityMap", {});
this.set("array", {});
const ServiceType = Ember.Service || Ember.Object;
var Store = ServiceType.extend({
init() {
this._super(...arguments);
this.reset();
},
clear: function(type) {
if(type === undefined) {
this.init();
reset() {
this.set("recompute", Ember.A());
this.set("filtersMap", {});
this.set("identityMap", {});
this.set("array", {});
},
clear(type) {
if (type === undefined) {
this.reset();
}
delete this.get("identityMap")[type];
arrayForType(type, this).clear();
Ember.run.once(() => {
this.scheduleUpdate(type);
});
this.scheduleUpdate(type);
},
remove: function(type, id) {
remove(type, id) {
var record = this._findById(type, id);

@@ -57,12 +67,10 @@ if (record) {

arrayForType(type, this).removeObject(record);
Ember.run.once(() => {
this.scheduleUpdate(type);
});
this.scheduleUpdate(type);
}
},
push: function(type, data) {
push(type, data) {
var record = this._findById(type, data.id);
if (record) {
record.setProperties(data);
setProperties(record, data);
} else {

@@ -72,31 +80,43 @@ record = buildRecord(type, data, this);

Ember.run.once(() => {
this.scheduleUpdate(type);
});
this.scheduleUpdate(type);
return record;
},
scheduleUpdate: function(type) {
scheduleUpdate(type) {
var recompute = this.get("recompute");
recompute.push(type);
Ember.run.scheduleOnce("actions", this, "updateFilters");
recompute.addObject(type);
run.scheduleOnce('actions', this, 'updateFilters');
},
updateFilters: function() {
updateFilters() {
var recompute = this.get("recompute");
var filtersMap = this.get("filtersMap");
Object.keys(filtersMap).forEach(function(type) {
Object.keys(filtersMap).forEach((type) => {
var filters = filtersMap[type] || [];
filters.forEach(function(func) {
if(Ember.$.inArray(type, recompute) > -1) {
var updatedContent = func.updateContent();
func.set('content', Ember.A(updatedContent));
filters.forEach((recordArray) => {
if (recompute.contains(type)) {
var updatedContent = recordArray.updateContent();
recordArray.set("content", updatedContent);
}
});
});
this.set("recompute", []);
this.set("recompute", Ember.A());
},
find: function(type, options) {
_unsubscribe(...args) {
var updatedFiltersMap;
var filterIds = Ember.A(args.map((func) => func.id));
var filtersMap = this.get("filtersMap");
Object.keys(filtersMap).forEach((type) => {
var filters = filtersMap[type] || [];
updatedFiltersMap = filters.filter((func) => !filterIds.contains(func.id));
filtersMap[type] = updatedFiltersMap;
});
},
find(type, options) {
if (typeof options === "undefined") {
return this._findAllProxy(type);
}
if (options instanceof Function) {

@@ -107,57 +127,47 @@ // var computed_keys = arguments[2] || [];

}
if (typeof options === "object") {
var params = Object.keys(options);
Ember.assert("No key was found in the filter options", params.length);
assert("No key was found in the filter options", params.length);
var attr = params[0];
var value = options[attr];
var func = function(m) {
return m.get(attr) === value;
};
return this._findWithFilterFunc(type, func);
return this._findWithFilterFunc(type, function filterFunction(m) {
return get(m, attr) === value;
});
}
return this._findByIdComputed(type, options);
},
_findById: function(type, id) {
findOne(type) {
return RecordProxy.create({
store: this,
type: type,
source: this._findAll(type),
compute() {
return this.get("source").objectAt(0);
}
});
},
_findById(type, id) {
var identityMap = identityMapForType(type, this);
return identityMap[id] || null;
},
_findAll: function(type) {
_findAll(type) {
return arrayForType(type, this);
},
_findAllProxy: function(type) {
return Ember.ArrayProxy.extend({
push: function(type, data) {
return this.push(type, data);
}.bind(this, type),
remove: function(type, id) {
this.remove(type, id);
}.bind(this, type),
content: Ember.computed(function () {
return Ember.A(this.get("source"));
})
}).create({
source: this._findAll(type)
_findAllProxy(type) {
return RecordArray.create({
type: type,
store: this,
source: this._findAll(type)
});
},
_findWithFilterFunc: function(type, filter_func) {
var func = Ember.ArrayProxy.extend({
push: function(type, data) {
return this.push(type, data);
}.bind(this, type),
remove: function(type, id) {
this.remove(type, id);
}.bind(this, type),
updateContent: function() {
var source = this.get("source");
var filter_func = this.get("filter_func");
return source.filter(filter_func);
},
content: Ember.computed(function () {
var source = this.get("source");
var filter_func = this.get("filter_func");
return Ember.A(source.filter(filter_func));
})
}).create({
_findWithFilterFunc(type, filter_func) {
var func = FilteredRecordArray.create({
type: type,
store: this,
id: Ember.uuid(),
filter_func: filter_func,

@@ -172,3 +182,3 @@ source: this._findAll(type)

},
_coerceId: function(id) {
_coerceId(id) {
var numberId = parseInt(id, 10);

@@ -180,57 +190,15 @@ if (numberId && numberId.toString().length === id.toString().length) {

},
_findByIdComputed: function(type, id) {
var store = this;
_findByIdComputed(type, id) {
var actualId = this._coerceId(id);
return Ember.ObjectProxy.extend({
content: Ember.computed("source.[]", function() {
var filter_value = this.get("filter_value");
var list = Ember.A(this.get("source").filterBy("id", filter_value));
return list.objectAt(0);
})
}).create({
return RecordProxy.create({
store: this,
type: type,
filter_value: actualId,
source: this._findAll(type),
init: function () {
var model = getOwner(store).lookup("model:" + type);
for(var method in model) {
if(typeof model[method] === "function") {
if(!this[method]) {
this.proxyMethod(method);
}
}
}
},
proxyMethod: function(method) {
this[method] = function() {
var content = this.get("content");
return content[method].apply(content, arguments);
};
compute() {
var filter_value = this.get("filter_value");
return this.get("source").findBy("id", filter_value);
}
});
},
findOne: function(type) {
var store = this;
return Ember.ObjectProxy.extend({
content: Ember.computed("source.[]", function() {
return this.get("source").objectAt(0);
})
}).create({
source: this._findAll(type),
init: function () {
var model = getOwner(store).lookup("model:" + type);
for(var method in model) {
if(typeof model[method] === "function") {
if(!this[method]) {
this.proxyMethod(method);
}
}
}
},
proxyMethod: function(method) {
this[method] = function() {
var content = this.get("content");
return content[method].apply(content, arguments);
};
}
});
}

@@ -237,0 +205,0 @@ });

ember-cli-simple-store Changelog
==============================
3.6.0
-----
* [DOCS]: Update README.md
([commit](https://github.com/toranb/ember-cli-simple-store/commit/d7bc3808325785cd61f80707fb4db1e41c7e1d68))
* [FEATURE]: Conditionally disable initializer
([commit](https://github.com/toranb/ember-cli-simple-store/commit/edf2d41b60ab2959224146dee4169e9805189a7a))
* [DEPENDENCY]: Lock jQuery to 1.11.3
([commit](https://github.com/toranb/ember-cli-simple-store/commit/16de403f8b2eb7e3f115a83f8ff8f6402d02243c))
* [DOCS]: Fix readme examples
([commit](https://github.com/toranb/ember-cli-simple-store/commit/2de430133dd9345de0034124880bf1c42fcef451))
3.5.0

@@ -5,0 +21,0 @@ -----

@@ -0,1 +1,2 @@

/*jshint node:true*/
module.exports = {

@@ -2,0 +3,0 @@ scenarios: [

@@ -0,1 +1,2 @@

/*jshint node:true*/
'use strict';

@@ -2,0 +3,0 @@

{
"name": "ember-cli-simple-store",
"version": "4.0.0-beta.6",
"version": "4.0.0-beta.7",
"description": "ember-cli addon that provides a simple identity map for ember.js web applications",

@@ -28,24 +28,19 @@ "directories": {

"devDependencies": {
"broccoli-asset-rev": "^2.1.2",
"broccoli-funnel": "^0.2.3",
"ember-cli": "1.13.8",
"ember-cli-app-version": "0.5.0",
"ember-cli-content-security-policy": "0.4.0",
"ember-cli-dependency-checker": "^1.0.1",
"ember-cli-htmlbars": "0.7.9",
"ember-cli-htmlbars-inline-precompile": "^0.2.0",
"ember-cli": "1.13.14",
"ember-cli-app-version": "^1.0.0",
"ember-cli-dependency-checker": "^1.1.0",
"ember-cli-htmlbars": "^1.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-inject-live-reload": "^1.3.1",
"ember-cli-qunit": "^1.0.0",
"ember-cli-release": "0.2.3",
"ember-cli-sri": "^1.0.3",
"ember-cli-qunit": "^1.0.4",
"ember-cli-release": "0.2.8",
"ember-cli-sri": "^1.2.0",
"ember-cli-uglify": "^1.2.0",
"ember-disable-proxy-controllers": "^1.0.0",
"ember-export-application-global": "^1.0.3",
"ember-disable-proxy-controllers": "^1.0.1",
"ember-export-application-global": "^1.0.4",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-cli-injection": "1.1.0",
"ember-try": "0.0.6",
"es5-shim": "^4.0.5"
"ember-try": "0.0.8"
},
"dependencies": {
"ember-cli-babel": "^5.1.3",
"ember-cli-babel": "^5.1.5",
"ember-getowner-polyfill": "^1.0.0"

@@ -52,0 +47,0 @@ },

@@ -11,2 +11,6 @@ # ember-cli-simple-store

## The stable readme is below (for anyone using the v3 series)
https://github.com/toranb/ember-cli-simple-store/blob/b317b00e0d61486cf47a40765991886e416638ba/README.md
## Installation

@@ -27,3 +31,3 @@

store.push("person", {id: 1, name: "toran"});
simpleStore.push("person", {id: 1, name: "toran"});
```

@@ -34,3 +38,3 @@

store.remove("person", 123);
simpleStore.remove("person", 123);
```

@@ -41,3 +45,3 @@

store.find("person");
simpleStore.find("person");
```

@@ -48,3 +52,3 @@

store.find("person", 123);
simpleStore.find("person", 123);
```

@@ -55,3 +59,3 @@

store.find("person", {account_id: 789});
simpleStore.find("person", {account_id: 789});
```

@@ -67,3 +71,3 @@

}
store.find("person", filter, ["salary", "name"]);
simpleStore.find("person", filter);
```

@@ -74,3 +78,3 @@

store.findOne("person");
simpleStore.findOne("person");
```

@@ -81,3 +85,3 @@

store.clear("person");
simpleStore.clear("person");
```

@@ -88,3 +92,3 @@

store.clear();
simpleStore.clear();
```

@@ -103,21 +107,22 @@

var PersonRepository = Ember.Object.extend({
find: function() {
var store = this.get("store");
simpleStore: Ember.inject.service(),
find() {
var simpleStore = this.get("simpleStore");
return PromiseMixin.xhr("/api/people/", "GET").then(function(response) {
response.forEach(function(person) {
store.push("person", person);
simpleStore.push("person", person);
});
return store.find("person");
return simpleStore.find("person");
});
},
findById: function(id) {
var store = this.get("store");
return store.find("person", id);
findById(id) {
var simpleStore = this.get("simpleStore");
return simpleStore.find("person", id);
},
insert: function(person) {
var store = this.get("store");
insert(person) {
var simpleStore = this.get("simpleStore");
var hash = {data: JSON.stringify(person)};
return new Ember.RSVP.Promise(function(resolve,reject) {
return PromiseMixin.xhr("/api/people/", "POST", hash).then(function(persisted) {
var inserted = store.push("person", persisted);
var inserted = simpleStore.push("person", persisted);
resolve(inserted);

@@ -129,3 +134,3 @@ }, function(err) {

},
update: function(person) {
update(person) {
var person_id = person.get("id");

@@ -136,4 +141,4 @@ var hash = {data: JSON.stringify(person)};

},
remove: function(person) {
var store = this.get("store");
remove(person) {
var simpleStore = this.get("simpleStore");
var person_id = person.get("id");

@@ -143,3 +148,3 @@ var endpoint = "/api/people/%@/".fmt(person_id);

return PromiseMixin.xhr(endpoint, "DELETE").then(function(arg) {
store.remove("person", person_id);
simpleStore.remove("person", person_id);
resolve(arg);

@@ -162,9 +167,10 @@ }, function(err) {

export default Ember.Route.extend({
model: function(params) {
var store = this.get("store");
var model = store.find("todo", params.todo_id);
var notes = store.find("note", {todo_id: params.todo_id});
simpleStore: Ember.inject.service(),
model(params) {
var simpleStore = this.get("simpleStore");
var model = simpleStore.find("todo", params.todo_id);
var notes = simpleStore.find("note", {todo_id: params.todo_id});
return Ember.RSVP.hash({model: model, notes: notes});
},
setupController: function(controller, hash) {
setupController(controller, hash) {
controller.setProperties({

@@ -180,3 +186,3 @@ "model": hash.model,

* you need to inject the store instance into each class that does data access (service/repository/route/controller)
* you need to inject the simpleStore instance into each class that does data access (service/repository/route/controller)
* you need to write each xhr yourself and pull objects from the store / push objects into the store

@@ -238,34 +244,2 @@

## Support for working in parallel with ember-data
Disabling auto-injection of the store is optional, but useful in the scenario where you need to run `ember-cli-simple-store` and `ember-data` in parallel -- since they both attempt to register a type against `store:main`. In order to work around this, manually register and inject `ember-cli-simple-store/store` under a new name.
```js
// config/environment.js
'use strict';
module.exports = function() {
return {
'ember-cli-simple-store': {
disableAutoInject: true
}
};
};
```
```js
// app/initializers/ember-cli-simple-store.js
import SimpleStore from 'ember-cli-simple-store/store';
export default {
name: 'override-simple-store',
initialize() {
var app = arguments[1] || arguments[0];
app.register('simpleStore:main', SimpleStore);
app.inject('controller', 'simpleStore', 'simpleStore:main');
app.inject('route', 'simpleStore', 'simpleStore:main');
}
};
```
## Example applications

@@ -272,0 +246,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc