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

Allow multiple converters to be registered with addConverter #563

Merged
merged 2 commits into from
Jun 29, 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
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