Skip to content

Commit

Permalink
Merge pull request #205 from canjs/let-context-prototype
Browse files Browse the repository at this point in the history
removing functions added by can-event-queue from LetContext.prototype
  • Loading branch information
phillipskevin authored Jul 9, 2019
2 parents 28b3661 + dd84ece commit 2623264
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
6 changes: 1 addition & 5 deletions can-view-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var canReflect = require("can-reflect");
var canLog = require('can-log/dev/dev');
var defineLazyValue = require('can-define-lazy-value');
var stacheHelpers = require('can-stache-helpers');
var SimpleMap = require('can-simple-map');
var LetContext = require('./let-context');


// ## Helpers
Expand All @@ -30,10 +30,6 @@ function returnFalse(){
return false;
}

// ### LetContext
// Instances of this are used to create a `let` variable context.
var LetContext = SimpleMap.extend("LetContext",{});

// ## Scope
// Represents a node in the scope tree.
function Scope(context, parent, meta) {
Expand Down
52 changes: 52 additions & 0 deletions let-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var SimpleMap = require('can-simple-map');

// ### LetContext
// Instances of this are used to create a `let` variable context.

// Like Object.create, but only keeps Symbols and properties in `propertiesToKeep`
function objectCreateWithSymbolsAndSpecificProperties(obj, propertiesToKeep) {
var newObj = {};

// copy over all Symbols from obj
if ("getOwnPropertySymbols" in Object) {
Object.getOwnPropertySymbols(obj).forEach(function(key) {
newObj[key] = obj[key];
});
}

// copy over specific properties from obj (also fake Symbols properties for IE support);
Object.getOwnPropertyNames(obj).forEach(function(key) {
if (propertiesToKeep.indexOf(key) >= 0 || key.indexOf("@@symbol") === 0) {
newObj[key] = obj[key];
}
});

return Object.create(newObj);
}

var LetContext = SimpleMap.extend("LetContext", {});
LetContext.prototype = objectCreateWithSymbolsAndSpecificProperties(SimpleMap.prototype, [
// SimpleMap properties
"setup",
"attr",
"serialize",
"get",
"set",
"log",
// required by SimpleMap properties
"dispatch",
// Construct properties (not added by can-event-queue)
"constructorExtends",
"newInstance",
"_inherit",
"_defineProperty",
"_overwrite",
"instance",
"extend",
"ReturnValue",
"setup",
"init"
]);
LetContext.prototype.constructor = LetContext;

module.exports = LetContext;
8 changes: 8 additions & 0 deletions test/scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,15 @@ QUnit.test("able to read partials", function(assert) {
var result = scope.get("myPartial");

assert.equal(result, myPartial, "read the value");
});

QUnit.test("properties can shadow functions on can-event-queue/map when there is a LetContext", function(assert) {
var scope = new Scope({
one: "the one property"
})
.addLetContext();

assert.equal(scope.get("one"), "the one property", "read the value");
});

require("./variable-scope-test");
Expand Down

0 comments on commit 2623264

Please sign in to comment.