Skip to content
Browse files

- removed the usePlural argument from overload[G/S]etter.

 - added the forceObjectResult argument to overloadGetter, so that plural getters always return an object, regardless of the number of arguments passed.
 - reverted the chain change to Array:each

Signed-off-by: Valerio Proietti <kamicane@gmail.com>
  • Loading branch information...
1 parent 7c98073 commit 627dad3dd2f35cb15993175270e6f2670c3f0887 @kamicane kamicane committed Apr 11, 2011
Showing with 17 additions and 21 deletions.
  1. +17 −21 Source/Core/Core.js
View
38 Source/Core/Core.js
@@ -31,11 +31,11 @@ var slice = Array.prototype.slice,
for (var i in {toString: 1}) enumerables = null;
if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'];
-Function.prototype.overloadSetter = function(usePlural){
+Function.prototype.overloadSetter = function(){
var self = this;
return function(a, b){
if (a == null) return this;
- if (usePlural || typeof a != 'string'){
@cpojer
MooTools member
cpojer added a note Apr 11, 2011

For plural methods, this was actually really useful. I use it a lot in my code.

Think setStyle and setStyles, where for the latter you only want to allow plural.

@cpojer
MooTools member
cpojer added a note Apr 11, 2011

Well, in this specific example I'd argue that setStyles is not necessary and setStyle should take singular and plural. But for a lot of other examples, usePlural is a valid use case for overloadSetter when you both provide singular and plural methods for the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (typeof a != 'string'){
for (var k in a) self.call(this, k, a[k]);
if (enumerables) for (var i = enumerables.length; i--;){
k = enumerables[i];
@@ -48,17 +48,22 @@ Function.prototype.overloadSetter = function(usePlural){
};
};
-Function.prototype.overloadGetter = function(usePlural){
+Function.prototype.overloadGetter = function(forceObjectResult){
+ // the forceObjectResult argument is used in those situations where a getter must always return a list of results.
+ // Usually, when autogenerating a multiGetter with a plural name (such as getStuff autogenerated from getThing) we want the plural version of the getter to always behave the same way, regardless of the arguments passed.
var self = this;
- return function(a){
- var args, result = {};
- if (usePlural || typeof a != 'string') args = a;
- else if (arguments.length > 1) args = arguments;
+ return function(argument){
+ var args;
+ if (typeof argument != 'string') args = argument; // the list of stuff to get is passed as an array
+ else if (arguments.length > 1) args = arguments; // the list of stuff to get is passed as arguments
+ else if (forceObjectResult) args = [argument]; //only one argument was passed, as we excluded the other 2 options before, but we wrap it in an array so that the object return is used.
if (args){
+ var result = {};
for (var i = 0; i < args.length; i++) result[args[i]] = self.call(this, args[i]);
return result;
+ } else {
+ return self.call(this, a);
}
- return self.call(this, a);
};
};
@@ -264,20 +269,11 @@ Object.extend('forEach', function(object, fn, bind){
}
}).extend('each', Object.forEach);
-Array.implement({
-
- forEach: function(fn, context){
- for (var i = 0, l = this.length; i < l; i++){
- if (i in this) fn.call(context, this[i], i, this);
- }
- },
-
- each: function(fn, bind){
- Array.forEach(this, fn, bind);
- return this;
+Array.implement('forEach', function(fn, context){
+ for (var i = 0, l = this.length; i < l; i++){
+ if (i in this) fn.call(context, this[i], i, this);
}
-
-});
+}).alias('each', 'forEach');
@cpojer
MooTools member
cpojer added a note Apr 11, 2011

Why? I liked that .each is chainable. We should definitely keep it.

@ibolmo
MooTools member
ibolmo added a note Apr 11, 2011

Ditto. I like to use it this way:

var widgets = $$('.widgets').each(Widget.create);
@appden
MooTools member
appden added a note Apr 12, 2011

Yea reverting that is not gonna work for me, or anybody, really. :)

Also, these things should be separated into distinct commits. There's no reason to group them together like this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
// Array & Object cloning

0 comments on commit 627dad3

Please sign in to comment.
Something went wrong with that request. Please try again.