Skip to content

Commit

Permalink
Changes to address canjs#3182. These are the same changes in canjs/ca…
Browse files Browse the repository at this point in the history
…n-list#38 with the addition of modifying the map/list code properly pass the client's context if it is provided.
  • Loading branch information
dtomack committed May 2, 2017
1 parent f4c61dc commit 7c46791
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ steal("can/util", "can/map", "can/map/bubble.js","can/map/map_helpers.js",functi
self = this,
filtered;
this.each(function(item, index, list){
filtered = callback.call( thisArg | self, item, index, self);
filtered = callback.call( thisArg || self, item, index, self);
if(filtered){
filteredList.push(item);
}
Expand All @@ -870,7 +870,7 @@ steal("can/util", "can/map", "can/map/bubble.js","can/map/map_helpers.js",functi
var filteredList = new can.List(),
self = this;
this.each(function(item, index, list){
var mapped = callback.call( thisArg | self, item, index, self);
var mapped = callback.call( thisArg || self, item, index, self);
filteredList.push(mapped);

});
Expand Down
30 changes: 27 additions & 3 deletions list/list_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,31 @@ steal("can/util", "can/list", "can/test", "can/compute", "steal-qunit", function
}, 10 );
}));
});




test('filter with context', function(){
var l = new can.List([{id: 1}]);
var context = {};
var contextWasCorrect = false;

l.filter(function(){
contextWasCorrect = (this === context);
return true;
}, context);

equal(contextWasCorrect, true, "context was correctly passed");
});

test('map with context', function(){
var l = new can.List([{id: 1}]);
var context = {};
var contextWasCorrect = false;

l.map(function(){
contextWasCorrect = (this === context);
return true;
}, context);

equal(contextWasCorrect, true, "context was correctly passed");
});

});
8 changes: 4 additions & 4 deletions map/list/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
steal('can/util', 'can/map', 'can/list', 'can/compute', function (can) {
can.extend(can.List.prototype, {
filter: function (callback) {
filter: function (callback, thisArg) {
// The filtered list
var filtered = new this.constructor();
var self = this;
Expand All @@ -20,7 +20,7 @@ steal('can/util', 'can/map', 'can/list', 'can/compute', function (can) {
};
// a can.compute that executes the callback
var compute = can.compute(function () {
return callback(element, self.indexOf(element), self);
return callback.call(thisArg || self, element, self.indexOf(element), self);
});
// Update the filtered list on any compute change
compute.bind('change', binder);
Expand Down Expand Up @@ -48,14 +48,14 @@ steal('can/util', 'can/map', 'can/list', 'can/compute', function (can) {
this.forEach(generator);
return filtered;
},
map: function (callback) {
map: function (callback, thisArg) {
var mapped = new can.List();
var self = this;
// Again, lets run a generator function
var generator = function (element, index) {
// The can.compute for the mapping
var compute = can.compute(function () {
return callback(element, index, self);
return callback.call(thisArg || self, element, index, self);
});
compute.bind('change', function (ev, val) {
// On change, replace the current value with the new one
Expand Down

0 comments on commit 7c46791

Please sign in to comment.