-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move store.filter into an addon #3274
Comments
cc @fivetanley |
@runspired commented moved form #3298 is copied below:
Heartily support of this, it would also show a pattern for extending ember-data features. For instance, I currently crudely add methods to store for lookups based on non-ID keys (unique or just simple K=>V matching) e.g. /**
* @method getByKey
*
* @param typeName
* @param key
* @param value
* @returns {*} A single or array of records from store if available
*/
Store.getByKey = function getByKey(typeName, key, value) {
return Store.filter(typeName, function (record) {
return record.get(key) === value;
}).then(function (matches) {
if (matches.get('length')) {
return matches;
}
return null;
});
};
/**
* @method getByUniqueKey | return a record based on a unique key
*
* @param typeName
* @param key
* @param value
* @returns {*} A single record from store if only one exists for the key, else null
*/
Store.getByUniqueKey = function getByUniqueKey(typeName, key, value) {
return Store.filter(typeName, function filterKnownRecords(record) {
return record.get(key) === value;
}).then(function (matches) {
assert("You looked up an item in the store by unique key '" + key + "' that was not a unique value '" + value + "'.", matches.get('length') <= 1);
if (matches.get('length') === 1) {
return matches.objectAt(0);
}
return null;
});
};
/**
* @method findByKey
*
* @param typeName
* @param key
* @param value
* @returns {*} A single or array of records from store if available, a single or array of records from server if not
*/
Store.findByKey = function findByKey(typeName, key, value) {
return Store.filter(typeName, function filterKnownRecords(record) {
return record.get(key) === value;
}).then(function (matches) {
if (matches.get('length')) {
return matches;
}
var query = {};
query[key] = value;
return Store.find(typeName, query).then(function (records) {
return records;
});
});
};
/**
* @method findByUniqueKey
*
* @param typeName
* @param key
* @param value
* @returns {*} A single or array of records from store if available, a single or array of records from server if not
*
* TODO add assertions for uniqueness
*/
Store.findByUniqueKey = function findByUniqueKey(typeName, key, value) {
return Store.filter(typeName, function filterKnownRecords(record) {
return record.get(key) === value;
}).then(function (matches) {
assert("You looked up an item in the store by unique key '" + key + "' that was not a unique value '" + value + "'.", matches.get('length') <= 1);
if (matches.get('length')) {
return matches.objectAt(0);
}
var query = {};
query[key] = value;
return Store.find(typeName, query).then(function (records) {
assert("You looked up an item from the server by unique key '" + key + "' that was not a unique value '" + value + "'.", records.get('length') <= 1);
if (records.get('length')) {
return records.objectAt(0);
}
return null;
});
});
}; |
What's the suggestion for avoiding Thanks! |
Actually just found this: https://github.com/ember-data/ember-data-filter |
It was moved |
We did not come up with solutions for store.filter, and the memory leak problemshould me move store.filter into an addon? And discourage the use? Or just keep in and hope for SemVer compatible fixes in the future
Tomhuda, I trust that you remember what the filter issue is, currently store.filter is kept around forever and leaks all over the place and keeps recomputing
The text was updated successfully, but these errors were encountered: