Skip to content

Commit

Permalink
Merge pull request #563 from canjs/mult-converter
Browse files Browse the repository at this point in the history
Allow multiple converters to be registered with addConverter
  • Loading branch information
matthewp authored Jun 29, 2018
2 parents 42b06c9 + e00e899 commit f6c0b2b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 11 additions & 0 deletions helpers/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ function makeConverter(getterSetter){
};
}

var converterPackages = new WeakMap();
helpers.addConverter = function(name, getterSetter) {
if(typeof name === "object") {
if(!converterPackages.has(name)) {
converterPackages.set(name, true);
canReflect.eachKey(name, function(getterSetter, name) {
helpers.addConverter(name, getterSetter);
});
}
return;
}

var helper = makeConverter(getterSetter);
helper.isLiveBound = true;
helpers.registerHelper(name, helper );
Expand Down
30 changes: 30 additions & 0 deletions test/expression-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,36 @@ QUnit.test("addConverter helpers push and pull multiple values", function () {
deepEqual(data.attr("list").attr(), [1,2,3,5], 'push converter called');
});

QUnit.test("Can register multiple converters at once with addConverter", function(){
QUnit.expect(2);
var converters = {
"converter-one": {
get: function(){
QUnit.ok(true, "converter-one called");
},
set: function(){}
},
"converter-two": {
get: function(){
QUnit.ok(true, "converter-two called");
},
set: function(){}
}
};

helpers.addConverter(converters);

var data = new SimpleMap({
person: "Matthew"
});
var scope = new Scope(data);
var parentExpression = expression.parse("converter-one(person)",{baseMethodType: "Call"});
parentExpression.value(scope).get();

parentExpression = expression.parse("converter-two(person)",{baseMethodType: "Call"});
parentExpression.value(scope).get();
});

test('foo().bar', function() {
// expression.ast
var ast4 = expression.ast("foo().bar");
Expand Down
4 changes: 2 additions & 2 deletions test/stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ require('../helpers/-debugger-test');
require('./nodelist-test');
require('../helpers/-each-test');
require('./section-test');
var stache = require('can-stache');
var core = require('can-stache/src/mustache_core');
var stache = require('../can-stache');
var core = require('../src/mustache_core');
var clone = require('steal-clone');
var canSymbol = require("can-symbol");
var canReflect = require("can-reflect");
Expand Down

0 comments on commit f6c0b2b

Please sign in to comment.