Permalink
Browse files

Merge remote-tracking branch 'upstream/master' into pushState

  • Loading branch information...
2 parents 7e1c3f2 + a7583f2 commit 63d260eba026d5fd61e517aebbcc0481dcdc35b8 @thecountofzero thecountofzero committed Jul 24, 2012
View
@@ -237,11 +237,11 @@ steal("can/util/string",function( $ ) {
_fullName = can.underscore(fullName.replace(/\./g, "_")),
_shortName = can.underscore(shortName);
- //@steal-remove-start
+ //!steal-remove-start
if(current[shortName]){
steal.dev.warn("class.js There's already something called "+fullName)
}
- //@steal-remove-end
+ //!steal-remove-end
current[shortName] = Constructor;
}
View
@@ -538,7 +538,6 @@ steal('can/construct', function( $ ) {
* @return {Integer} The id of the binding in this._bindings
*/
on: function( el, selector, eventName, func ) {
-
if ( ! el ) {
// Adds bindings.
@@ -581,7 +580,13 @@ steal('can/construct', function( $ ) {
selector = el;
el = this.element;
}
-
+
+ if(func === undefined) {
+ func = eventName;
+ eventName = selector;
+ selector = null;
+ }
+
if ( typeof func == 'string' ) {
func = can.Control._shifter(this,func);
}
View
@@ -150,7 +150,26 @@ if(!(isOpera && isDojo)){
var called = false,
DelegateTest= can.Control({
click: function() {}
- })
+ }),
+ Tester = can.Control({
+ init : function(el, ops) {
+ this.on(window, 'click', function(ev) {
+ ok(true, 'Got window click event');
+ });
+
+ this.on(window, 'click', 'clicked');
+ this.on('click', function() {
+ ok(true, 'Directly clicked element');
+ });
+ this.on('click', 'clicked');
+ },
+
+ clicked : function() {
+ ok(true, 'Controller action delegated click triggered, too');
+ }
+ }),
+ div = document.createElement('div'),
+ rb = new Tester(div);
can.append( can.$("#qunit-test-area"), "<div id='els'><span id='elspan'><a href='#' id='elsa'>click me</a></span></div>")
@@ -166,6 +185,9 @@ if(!(isOpera && isDojo)){
can.trigger( can.$("#els a"), 'click')
ok(called, "delegate works")
can.remove( els )
+
+ can.trigger(div, 'click');
+ can.trigger(window, 'click');
});
}
@@ -285,8 +307,8 @@ test("actions provide method names", function(){
});
var item1 = {},
- item2 = {}
- rb = new Tester( document.createElement('div'), {item1: item1, item2: item2} );
+ item2 = {},
+ rb = new Tester( document.createElement('div'), {item1: item1, item2: item2} );
can.trigger(item1, "foo");
can.trigger(item2, "bar");
View
@@ -146,15 +146,16 @@ can.Control.plugin = function(pluginname){
$.fn[pluginname] = function(options){
var args = makeArray(arguments), //if the arg is a method on this control
- isMethod = typeof options == "string" && $.isFunction(control.prototype[options]), meth = args[0];
- return this.each(function(){
+ isMethod = typeof options == "string" && $.isFunction(control.prototype[options]), meth = args[0],
+ returns;
+ this.each(function(){
//check if created
var plugin = can.$(this).control(control);
if (plugin) {
if (isMethod) {
// call a method on the control with the remaining args
- plugin[meth].apply(plugin, args.slice(1));
+ returns = plugin[meth].apply(plugin, args.slice(1));
}
else {
// call the plugin's update method
@@ -166,6 +167,7 @@ can.Control.plugin = function(pluginname){
control.newInstance.apply(control, [this].concat(args));
}
});
+ return returns !== undefined ? returns : this;
};
}
}
View
@@ -31,23 +31,28 @@ different approaches.
Once a controller is initialized on a DOM element, you can invoke a method by calling
the plugin with the method name followed by the parameters for that method.
+When calling a method and it returns a value other than `undefined`, this value will
+be returned. Otherwise the method call will return the jQuery object (for chaining).
var MyTodo = can.Control({
pluginName : 'my_todo'
}, {
addTask: function(name, task){
- this.element.append(name + " " + task)
+ this.element.append(name + ": " + task)
+ },
+
+ getTasks : function() {
+ return this.element.text();
}
});
- //- inits the widget
+ // Initialize MyTodo
$('.my_todo').my_todo();
- //- calls the method `update`
- $('.my_todo').my_todo("addTask", 'Brian', 'Sweep garage');
-
-Keep in mind that this approach follows the jQuery plugin convention and returns a jQuery object,
-not the methods return value.
+ // Calls addTask, returns the jQuery element
+ $('.my_todo').my_todo("addTask", 'Brian', 'Sweep garage') // -> jQuery
+ // Calls getTasks, returns the element text
+ $('.my_todo').my_todo("getTasks") // -> Brian: Sweep garage
You can also retrieve the control instance and invoke the method directly.
@@ -56,4 +56,23 @@
equal(ta.control().options.testop, 'testing', 'Test option has been extended properly');
});
+
+ test('calling methods', function() {
+ var Control = can.Control({
+ pluginName : 'callTest'
+ }, {
+ returnTest : function() {
+ return 'Hi ' + this.name;
+ },
+
+ setName : function(name) {
+ this.name = name;
+ }
+ });
+
+ var ta = can.$("<div/>").appendTo($("#qunit-test-area"));
+ ta.callTest();
+ ok(ta.callTest('setName', 'Tester') instanceof jQuery, 'Got jQuery element as return value');
+ equal(ta.callTest('returnTest'), 'Hi Tester', 'Got correct return value');
+ });
})();
@@ -2,7 +2,7 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../../funcunit/qunit/qunit.css" />
- <title>route QUnit Test</title>
+ <title>can.Control.plugin QUnit Test</title>
<script type='text/javascript' src='../../../steal/steal.js'></script>
</head>
<body>
Oops, something went wrong.

0 comments on commit 63d260e

Please sign in to comment.