Permalink
Browse files

adds an extra level of indirection to control '.foo click' : 'methodN…

…ame'
  • Loading branch information...
justinbmeyer committed May 16, 2012
1 parent e460690 commit cab9b518ac0193431815ac0d34938f1168e45d5f
Showing with 52 additions and 7 deletions.
  1. +13 −6 control/control.js
  2. +19 −0 control/control_test.js
  3. +5 −1 control/route/route.js
  4. +15 −0 control/route/route_test.js
View
@@ -38,6 +38,9 @@ steal('can/construct', function( $ ) {
// Moves `this` to the first argument, wraps it with `jQuery` if it's an element
shifter = function shifter(context, name) {
var method = typeof name == "string" ? context[name] : name;
+ if(!isFunction(method)){
+ method = context[method];
+ }
return function() {
context.called = name;
return method.apply(context, [this.nodeName ? can.$(this) : this].concat( slice.call(arguments, 0)));
@@ -75,11 +78,7 @@ steal('can/construct', function( $ ) {
// Calculate and cache actions.
control.actions = {};
-
for ( funcName in control.prototype ) {
- if (funcName == 'constructor' || ! isFunction(control.prototype[funcName]) ) {
- continue;
- }
if ( control._isAction(funcName) ) {
control.actions[funcName] = control._action(funcName);
}
@@ -93,7 +92,15 @@ steal('can/construct', function( $ ) {
* @return {Boolean} truthy if an action or not
*/
_isAction: function( methodName ) {
- return !! ( special[methodName] || processors[methodName] || /[^\w]/.test(methodName) );
+
+ var val = this.prototype[methodName],
+ type = typeof val;
+ // if not the constructor
+ return (methodName !== 'constructor') &&
+ // and is a function or links to a function
+ ( type == "function" || (type == "string" && isFunction(this.prototype[val] ) ) ) &&
+ // and is in special, a processor, or has a funny character
+ !! ( special[methodName] || processors[methodName] || /[^\w]/.test(methodName) );
},
// Takes a method name and the options passed to a control
// and tries to return the data necessary to pass to a processor
@@ -123,7 +130,7 @@ steal('can/construct', function( $ ) {
// If we don't have options (a `control` instance), we'll run this
// later.
- paramReplacer.lastIndex = 0;
+ paramReplacer.lastIndex = 0;
if ( options || ! paramReplacer.test( methodName )) {
// If we have options, run sub to replace templates `{}` with a
// value from the options or the window
View
@@ -273,4 +273,23 @@ test("on rebinding", 2, function(){
rb.on();
can.trigger(item2, "foo")
});
+
+test("actions provide method names", function(){
+ var Tester = can.Control({
+ "{item1} foo" : "food",
+ "{item2} bar" : "food",
+ food : function(item, ev, data){
+ ok(true, "food called")
+ ok(item === item1 || item === item2, "called with an item")
+ }
+ });
+
+ var item1 = {},
+ item2 = {}
+ rb = new Tester( document.createElement('div'), {item1: item1, item2: item2} );
+
+ can.trigger(item1, "foo");
+ can.trigger(item2, "bar");
+})
+
})()
View
@@ -14,8 +14,12 @@ steal('can/route','can/control', function(){
var d = can.route.attr();
delete d.route;
+ if(can.isFunction(controller[funcName])){
+ controller[funcName]( d )
+ }else {
+ controller[controller[funcName]](d)
+ }
- controller[funcName]( d )
}
}
can.route.bind( 'change', check );
@@ -32,4 +32,19 @@ test("routes changed", function () {
can.trigger(window, 'hashchange');
});
+test("route pointers", function(){
+ expect(1);
+ var tester = can.Control({
+ "foo/:bar route" : "meth",
+ meth : function(){
+ ok(true, "method pointer called")
+ }
+ });
+ new tester(document.body);
+ window.location.hash = '!foo/bar';
+ can.trigger(window, 'hashchange');
+
+})
+
+
})();

0 comments on commit cab9b51

Please sign in to comment.