Skip to content

Commit cab9b51

Browse files
committed
adds an extra level of indirection to control '.foo click' : 'methodName'
1 parent e460690 commit cab9b51

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

control/control.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ steal('can/construct', function( $ ) {
3838
// Moves `this` to the first argument, wraps it with `jQuery` if it's an element
3939
shifter = function shifter(context, name) {
4040
var method = typeof name == "string" ? context[name] : name;
41+
if(!isFunction(method)){
42+
method = context[method];
43+
}
4144
return function() {
4245
context.called = name;
4346
return method.apply(context, [this.nodeName ? can.$(this) : this].concat( slice.call(arguments, 0)));
@@ -75,11 +78,7 @@ steal('can/construct', function( $ ) {
7578

7679
// Calculate and cache actions.
7780
control.actions = {};
78-
7981
for ( funcName in control.prototype ) {
80-
if (funcName == 'constructor' || ! isFunction(control.prototype[funcName]) ) {
81-
continue;
82-
}
8382
if ( control._isAction(funcName) ) {
8483
control.actions[funcName] = control._action(funcName);
8584
}
@@ -93,7 +92,15 @@ steal('can/construct', function( $ ) {
9392
* @return {Boolean} truthy if an action or not
9493
*/
9594
_isAction: function( methodName ) {
96-
return !! ( special[methodName] || processors[methodName] || /[^\w]/.test(methodName) );
95+
96+
var val = this.prototype[methodName],
97+
type = typeof val;
98+
// if not the constructor
99+
return (methodName !== 'constructor') &&
100+
// and is a function or links to a function
101+
( type == "function" || (type == "string" && isFunction(this.prototype[val] ) ) ) &&
102+
// and is in special, a processor, or has a funny character
103+
!! ( special[methodName] || processors[methodName] || /[^\w]/.test(methodName) );
97104
},
98105
// Takes a method name and the options passed to a control
99106
// and tries to return the data necessary to pass to a processor
@@ -123,7 +130,7 @@ steal('can/construct', function( $ ) {
123130

124131
// If we don't have options (a `control` instance), we'll run this
125132
// later.
126-
paramReplacer.lastIndex = 0;
133+
paramReplacer.lastIndex = 0;
127134
if ( options || ! paramReplacer.test( methodName )) {
128135
// If we have options, run sub to replace templates `{}` with a
129136
// value from the options or the window

control/control_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,23 @@ test("on rebinding", 2, function(){
273273
rb.on();
274274
can.trigger(item2, "foo")
275275
});
276+
277+
test("actions provide method names", function(){
278+
var Tester = can.Control({
279+
"{item1} foo" : "food",
280+
"{item2} bar" : "food",
281+
food : function(item, ev, data){
282+
ok(true, "food called")
283+
ok(item === item1 || item === item2, "called with an item")
284+
}
285+
});
286+
287+
var item1 = {},
288+
item2 = {}
289+
rb = new Tester( document.createElement('div'), {item1: item1, item2: item2} );
290+
291+
can.trigger(item1, "foo");
292+
can.trigger(item2, "bar");
293+
})
294+
276295
})()

control/route/route.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ steal('can/route','can/control', function(){
1414

1515
var d = can.route.attr();
1616
delete d.route;
17+
if(can.isFunction(controller[funcName])){
18+
controller[funcName]( d )
19+
}else {
20+
controller[controller[funcName]](d)
21+
}
1722

18-
controller[funcName]( d )
1923
}
2024
}
2125
can.route.bind( 'change', check );

control/route/route_test.js

+15
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,19 @@ test("routes changed", function () {
3232
can.trigger(window, 'hashchange');
3333
});
3434

35+
test("route pointers", function(){
36+
expect(1);
37+
var tester = can.Control({
38+
"foo/:bar route" : "meth",
39+
meth : function(){
40+
ok(true, "method pointer called")
41+
}
42+
});
43+
new tester(document.body);
44+
window.location.hash = '!foo/bar';
45+
can.trigger(window, 'hashchange');
46+
47+
})
48+
49+
3550
})();

0 commit comments

Comments
 (0)