views:

53

answers:

2
+1  Q: 

MooTools Chaining

+1  A: 

What you are asking has nothing to do with chaining. this has no context, so your call fails. The solution you are not happy with is the way you will need to write it for other values/attributes, but for a straight forward change like this, write it this way:

$('buttonContainer').getElement('input').value += '  ';
Doug Neiner
+3  A: 

MooTools cannot rebind this on the fly for every method called. This would be impossible.

You have to understand that every single call to your chain is in the same scope, therefore this remains the same. jQuery and every single other framework have the same problem. If you want to do two operations on an element at the same time, you must store the object in a variable and then use that variable to reference the object exactly like you did in your second example:

var input = $('buttonContainer').getElement('input');
input.set('value', input.get('value') + '  ');

this can only change when the scope changes (which in JavaScript is always when you hit a brace {} enclosing a function). This is not a limitation of MooTools' chaining. It's the way JavaScript in general works.

Andrew Moore
@Andrew, I know what you are trying to say, but in JS you hit braces a lot (`if`, `for`, `while`, `switch`, `try`), and the *only* time it changes scope is when those braces are associated with a function.
Doug Neiner
@Doug: Thanks, small oversight on my part, I've edited the answer to correct this mistake.
Andrew Moore
+1 great answer Andrew, and congrats for hitting 10K rep!
Doug Neiner
@Doug: Thank you!
Andrew Moore