Created
March 10, 2016 16:49
-
-
Save runspired/3d9835deff2aa46e3a59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from "ember"; | |
let STEP_GUID = 0; | |
const { | |
A, | |
computed, | |
defineProperty, | |
get | |
} = Ember; | |
const Descriptor = computed(function() {}).constructor; | |
function ExpandedFilterByProperty(array, path, value) { | |
this.isDescriptor = true; | |
if (path.indexOf('@each') !== -1) { | |
throw new Error('Using @each in an expanded-filter-by is not allowed'); | |
} | |
let stepPath; | |
let subPath; | |
let stepName; | |
let leafValue; | |
let watchPath; | |
if (path.indexOf('.') !== -1) { | |
subPath = path.split('.'); | |
if (subPath.length > 2) { | |
throw new Error('Expanding @each more than two leafs in an expanded-filter-by is not allowed'); | |
} | |
// this is the new step we need to create for this computed to function | |
stepName = `-computed-filter-by-step-${STEP_GUID++}`; | |
leafValue = subPath.shift(); | |
stepPath = `${array}.@each.${leafValue}`; | |
// this is the new path that uses the step | |
watchPath = `${stepName}.@each.${subPath[0]}`; | |
} | |
this.setup = function(context /*, key*/) { | |
// creates the step | |
defineProperty(context, stepName, computed(stepPath, function() { | |
let v = this.get(array); | |
if (v) { | |
return v.map(function(i) { | |
return get(i, leafValue); | |
}); | |
} | |
return null; | |
})); | |
}; | |
this._dependentKeys = [watchPath]; | |
this._getter = function() { | |
let arr = new A(this.get(array)); | |
return arr.filterBy(path, value); | |
}; | |
this._meta = undefined; | |
this._readOnly = true; | |
this._suspended = undefined; | |
this._volatile = false; | |
} | |
ExpandedFilterByProperty.prototype = new Descriptor(); | |
export default function expandedFilterByProperty(array, path, value) { | |
return new ExpandedFilterByProperty(array, path, value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment