@nwhite:

However, your post has now made me think if there are a few ways to
make the syntax nicer using chains... As far as I see it there are 2
different sorts of 'chained' functions.

- Ones that require an immediate call to 'callChain' at the end (I
think you were catering to these in your 'run' function)

- Ones that need to call 'callChain' some time later, say after a
transition or request.

So what about making the syntax something like:

var myChain = new Chain();

var myFx = new Fx({onComplete: function() {myChain.callChain()}});

myChain.chain(
  new Chain.L(function() {myFx.start()}), // myFx will call 'callChain' 'later'
  new Chain.N(function() {console.log('Done')}) // Force callChain 'now'
);

I'm now almost just thinking out loud, but this then does lead the way
to other Chain.'something' functions. Say if you want the next item in
the chain to happen after *two* conditions are met. In my mooforum
post I describe a method using a 'dummy' function. However, what about
something having Chain.M (for 'Multiple'):

var myChain = new Chain();

var myFx = new Fx({onComplete: function() {myChain.callChain()}});
var myRequest= new Request({onSuccess: function() {myChain.callChain()}});

myChain.chain(
  new Chain.M(function() {myFx.start; myRequest.send();}, 2),
  new Chain.N(function() {console.log('Done transition and request')})
);

The above Chain.M has '2' as its second argument, and so will require
2 calls to callChain to move onto the next item in the 'outer' chain.
Or, the syntax could avoid the numerical argument by

myChain.chain(
  new Chain.M(function() {myFx.start()}, function() {myRequest.send()}),
  new Chain.N(function() {console.log('Done transition and request')})
);

And the Chain.M constructor could just count the number of arguments
to find out how many calls to callChain at that point would be
required to move the chain along.

Or is this all a bit pointless...? Is it even possible...?


On Mon, Apr 27, 2009 at 9:30 AM, Michal <michalchare...@gmail.com> wrote:
> @nwhite:
>
> I'm not too sure why such a 'run' function would be useful. If all the
> functions in the chain are to be called in order, immediately, then
> why go to the hassle of creating a chain for them? Why not just call
> the required functions in order?
>
> I thought that Chain is useful for when at least one of the conditions
> for moving to the next function in the chain is satisfied "later", say
> after a transition or Ajax request. In this case I suspect such a
> 'run' function would not give the desired effect.
>
>
> On Mon, Apr 27, 2009 at 8:30 AM, nwhite <changereal...@gmail.com> wrote:
>> @Michal - your reference info is great.
>>
>> I have heard/seen a lot of confusion/hassle around the need for 'callChain'
>> within chained methods for auto run chaining actions.
>>
>> The example below automatically calls the next function in the chain stack
>> without the need for using 'callChain'. It supports passing arguments to
>> each function in the chain and returning the value of the final function in
>> the chain.
>>
>> I think it makes the usage of chains a lot more convenient as it fully
>> decouples 'Chain' from the actual chained functions.
>>
>>
>>     Chain.implement({
>>         run : function(){
>>             while(this.$chain.length) var arguments =
>> this.$chain.shift().apply(this,$splat(arguments));
>>             return arguments;
>>         }
>>     });
>>
>>     var myChain = new Chain();
>>
>>     myChain.chain(
>>         function(x){ console.log('chain',x); return ++x; },
>>         function(x){ console.log('chain',x); return ++x; },
>>         function(x){ console.log('chain',x); return ++x; }
>>     );
>>
>>     var x =    myChain.run(1);
>>     console.log(x);
>>
>>     /* output
>>         chain 1
>>         chain 2
>>         chain 3
>>         4
>>     */
>>
>>
>>
>>
>> On Mon, Apr 27, 2009 at 12:10 AM, Michal Charemza <michalchare...@gmail.com>
>> wrote:
>>>
>>> Just for reference, there is also quite a long post over at MooForum
>>> discussing chaining
>>> http://www.mooforum.net/help12/chain-guide-t1347.html (erm... written by
>>> me!)
>>>
>>> Michal.
>>>
>>>
>>> On 27 Apr 2009, at 02:37, nutron wrote:
>>>
>>>> Chain has 2 main methods and you have to use both in order for it to do
>>>> anything:
>>>>
>>>> chain - pushes a function onto the stack
>>>> callChain - calls the next function on the stack
>>>>
>>>> so if you want to do:
>>>>
>>>> this.fn1().chain(this.fn2.bind(this));
>>>>
>>>> then fn1 has to have this in it:
>>>>
>>>> fn1: function(){
>>>>  ...some logic that you want to execute
>>>>  this.callChain();
>>>>  return this;
>>>> }
>>>>
>>>> On Sun, Apr 26, 2009 at 6:17 PM, nwhite (via Nabble)
>>>> <ml-user%2b93763-1341009...@...> wrote:
>>>>
>>>> if(this.options.preload){
>>>>   new Asset.images(['/images/myImage.png', '/images/myImage2.gif'], {
>>>>
>>>>
>>>>
>>>>      onComplete: this.setUp
>>>>
>>>>   });
>>>> }
>>>>
>>>>
>>>> On Sun, Apr 26, 2009 at 6:06 PM, mmjaeger <mmjae...@...> wrote:
>>>>
>>>> I'm not sure whether that would work.
>>>>
>>>> the following code actually does work but I hopped there is an easier
>>>> way to do it:
>>>>
>>>> /*
>>>>
>>>>               if (this.options.preload) {
>>>>                       var callback = new Chain();
>>>>                       this._preload(callback);
>>>>                       callback.chain (
>>>>                               function() {
>>>>                                       this.setUp();
>>>>                               }.bind(this)
>>>>                       )
>>>>               } else {
>>>>                       this.setUp();
>>>>               }*/
>>>>
>>>> On Apr 26, 5:17 pm, "asgaroth.belem" <asgaroth.be...@...> wrote:
>>>> > I think all you have to do is (not sure, sorry for any mistakes):
>>>> >
>>>> > this.function1().chain(this.function2());
>>>> >
>>>> > thats what chain is for, it wont execute function2 until function 1
>>>> > has ended. at least is what I think, i dont really use chain much.
>>>> >
>>>> > On 26 abr, 18:38, mmjaeger <mmjae...@...> wrote:
>>>> >
>>>> > > Hello
>>>> > > Hope somebody can help - I'm struggling with chaining two functions
>>>> >
>>>> > > this is the code I got:
>>>> >
>>>> > > this.function1().chain(
>>>> > >       function() {
>>>> > >           this.function2();
>>>> > >       }
>>>> > > );
>>>> >
>>>> > > function1 has an onComplete event - like this:
>>>> >
>>>> > > I put the following in there:
>>>> >
>>>> > > onComplete: function() {
>>>> > >     this.callChain();
>>>> >
>>>> > > }
>>>> >
>>>> > > function1 is basically a function to preload some image - when they
>>>> > > are loaded, I like to continue to execute function2.
>>>> >
>>>> > > Thank you in advance for your help.
>>>> >
>>>> > > What am I missing?
>>>>
>>>>
>>>> The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com
>>>>
>>>> View this message in context: Re: [Moo] Re: Struggling with chain
>>>> Sent from the MooTools Users mailing list archive at Nabble.com.
>>>
>>
>>
>

Reply via email to