Skip to content
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

refactoring Scope.prototype.hasKey to avoid using getPathsForKey #173

Merged
merged 2 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 12 additions & 39 deletions can-view-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ assign(Scope.prototype, {

// ## Scope.prototype.getPathsForKey
// Finds all paths that will return a value for a specific key
// NOTE: this is for development purposes only and is removed in production
getPathsForKey: function getPathsForKey(key) {
//!steal-remove-start
var paths = {};

var getKeyDefinition = function(obj, key) {
Expand Down Expand Up @@ -493,53 +495,24 @@ assign(Scope.prototype, {
});

return paths;
//!steal-remove-end
},

// ## Scope.prototype.hasKey
// returns whether or not this scope has the key
hasKey: function hasKey(key) {
// jshint maxdepth:5
var context = this._context;
var keyReads = observeReader.reads(key);
var propDefined = false;

if(typeof context === "object") {
if(!keyReads) {
propDefined = canReflect.hasKey(context, key);
} else {
var reads = keyReads, i = 0, readsLength = reads.length;
var read;
do {
read = reads[i];
if(canReflect.hasKey(context, read.key)) {
propDefined = true;

// Get the next context and continue to see if the key is defined.
context = canReflect.getKeyValue(context, read.key);

if(context) {
propDefined = false;
i++;
continue;
} else {
break;
}
}
break;
} while(i < readsLength);
}
}
var reads = observeReader.reads(key);
var readValue;

// if the prop isn't defined on the context, check if it is a supported "special" key
// like scope.top.<key> or scope.vm.<key>
if (!propDefined) {
var paths = this.getPathsForKey(key);
if (paths[key]) {
propDefined = true;
}
if (reads[0].key === "scope") {
// read properties like `scope.vm.foo` directly from the scope
readValue = observeReader.read(this, reads.slice(1), key);
} else {
// read normal properties from the scope's context
readValue = observeReader.read(this._context, reads, key);
}

return propDefined;
return readValue.foundLastParent && readValue.parentHasKey;
},

// ## Scope.prototype.getDataForScopeSet
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"can-simple-map": "^4.0.0",
"can-single-reference": "^1.0.0",
"can-stache-helpers": "^1.0.0",
"can-stache-key": "^1.2.0",
"can-stache-key": "^1.3.0",
"can-symbol": "^1.0.0"
},
"devDependencies": {
Expand Down