communication between multiple sammy-apps

168 views
Skip to first unread message

dpree

unread,
Dec 15, 2009, 1:07:17 PM12/15/09
to Sammy.js
hi folks!

currently, i use 2 different sammy apps on different webpages and they
just work fine :)

the problem: i have one webpage requiring the functionality of both
apps...

my approach is to run both apps parallely and to communicate between
routes..

var app1 = $.sammy(function(){
this.get('#/hello1', function(){
log('hello from 1');
this.redirect('#/hello2');
});
});
var app2 = $.sammy(function(){
this.get('#/hello2', function(){
log('hello from 2');
});
});

doing a '#/hello1' then should print both log-messages...

the problem: it occurs, that redirecting to '#/hello2' only works the
first time..


i presume that my approach is far off beeing "best practice".. so i
appreciate any suggestions whatsoever :)


cheers,
dpree

Aaron Quint

unread,
Dec 15, 2009, 3:15:18 PM12/15/09
to sam...@googlegroups.com
Hey dpree

Running two apps on the same page should work just fine (I've done it
in the past). What do you mean redirecting to hello2 only works the
first time? I would suggest if you really just have to pass messages
between the apps and not track seperate state, then use the bind() /
trigger() methods of sammy to send events back and forth.

--AQ


Aaron Quint
http://www.quirkey.com
> --
>
> You received this message because you are subscribed to the Google Groups "Sammy.js" group.
> To post to this group, send email to sam...@googlegroups.com.
> To unsubscribe from this group, send email to sammyjs+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sammyjs?hl=en.
>
>
>

dpree

unread,
Dec 15, 2009, 3:34:36 PM12/15/09
to Sammy.js
Hi Aaron,

thx for your reply!

like u said, two apps on the same page = no problem.

=> just when it comes to redirecting between them, i failed...

how to reproduce the failure:

(1) do a '/#hello1' by manually typing the browser-url
(2) route is grabbed by app1: logging 'hello from 1' and redirecting
to '/#hello2'
(3) SUCCESS: route is grabbed in app2: logging 'hello from 2'

then again:
(4) do '/#hello1' by manually typing the browser-url
(5) route is grabbed by app1... logging 'hello from 1' and redirecting
to '/#hello2'
(6) FAIL: route is not grabbed in app2

but however:
(7) do a '/#hello2' by manually typing the browser-url
(8) SUCCESS: route is grabbed in app2: logging 'hello from 2'

i suspect, that in step (6) the app2 is already in the '#/hello2'
state and, therefore, won't execute this route again?


however, bind/trigger could work, i'll try this later..

cheers,
dpree


On 15 Dez., 21:15, Aaron Quint <aa...@quirkey.com> wrote:
> Hey dpree
>
> Running two apps on the same page should work just fine (I've done it
> in the past). What do you mean redirecting to hello2 only works the
> first time? I would suggest if you really just have to pass messages
> between the apps and not track seperate state, then use the bind() /
> trigger() methods of sammy to send events back and forth.
>
> --AQ
>
> Aaron Quinthttp://www.quirkey.com

Aaron Quint

unread,
Dec 15, 2009, 4:17:35 PM12/15/09
to sam...@googlegroups.com
dpree - yeah, you're exactly right, because the 'last_location' of app
2 is #/hello2 it wont be triggered again by a redirect. FYI, you can
also define the 'same' route for multiple apps, e.g.:

var app1 = $.sammy(function() {
this.get('#/hello', function() {
this.log('hello from app1');
});
});

var app2 = $.sammy(function() {
this.get('#/hello', function() {
this.log('hello from app2');
});
});

when hitting #/hello should log
'hello from app1'
'hello from app2'

--AQ



Aaron Quint
http://www.quirkey.com

dpree

unread,
Dec 18, 2009, 8:26:31 AM12/18/09
to Sammy.js
Aaaron, thanks a lot for the hint at the same-route-thing!

actually i haven't tried this, just because i was afraid of asking too
much of sammy :)

also i got confused about some ideas like the following:

(1) =run=> #/firstHellofromApps1n2
---
(2.1) app1(/#firstHellofromApps1n2) =redirect=> #/hellofromApp1
(2.2) app1(/#hellofromApp1) =redirect=> #/secondHellofromApps1n2
(2.3) app1(#/secondHellofromApps1n2) => log(23)
(2.4) app2(#/secondHellofromApps1n2) => log(24)
---
(3.1) app2(/#firstHellofromApps1n2) =redirect=> #/hellofromApp2
(3.2) app2(/#hellofromapp2) =redirect=> #/secondHellofromApps1n2
(3.3) nothing happens here, as app1 is already uptodate
(3.4) app2(#/secondHellofromApps1n2) => log(34)
===
logged 23,24,34

or maybe i am wrong, and the order would look like:

(1) =run=> #/firstHellofromApps1n2
---
(2.1) app1(/#firstHellofromApps1n2) =redirect=> #/hellofromApp1
(2.2) app2(/#firstHellofromApps1n2) =redirect=> #/hellofromApp2
---
(3.1) app1(/#hellofromApp1) =redirect=> #/secondHellofromApps1n2
(3.2) app2(/#hellofromApp2) =redirect=> #/secondHellofromApps1n2
---
(4.1) app1(#/secondHellofromApps1n2) => log(1)
(4.2) app2(#/secondHellofromApps1n2) => log(2)
===
logged 41, 42


and i suspect a lot more difficult ideas to be out there :)

cheers,
dpree


On 15 Dez., 22:17, Aaron Quint <aa...@quirkey.com> wrote:
> dpree - yeah, you're exactly right, because the 'last_location' of app
> 2 is #/hello2 it wont be triggered again by a redirect. FYI, you can
> also define the 'same' route for multiple apps, e.g.:
>
> var app1 = $.sammy(function() {
>    this.get('#/hello', function() {
>     this.log('hello from app1');
>    });
>
> });
>
> var app2 = $.sammy(function() {
>    this.get('#/hello', function() {
>     this.log('hello from app2');
>    });
>
> });
>
> when hitting #/hello should log
> 'hello from app1'
> 'hello from app2'
>
> --AQ
>

> Aaron Quinthttp://www.quirkey.com

Aaron Quint

unread,
Dec 18, 2009, 3:42:43 PM12/18/09
to sam...@googlegroups.com
Well the one thing about having multiple apps, is that if you're using
the hash (#) to track state, both apps have to agree on being in the
same state at the same time as theres only one hash per document.
However, you can maitain the state in an internal variable, the
benefit being you can have multiple apps in multiple states on a
single page. This isnt super super easy now, but Sammy 0.4 (coming
soon) will support creating proxys that define how state is
maintained.

--AQ

Aaron Quint
http://www.quirkey.com

dpree

unread,
Dec 19, 2009, 5:05:37 AM12/19/09
to Sammy.js
Aaron, that sounds just awesome :)

ever since the first time i heard about sammy, the fact that there is
"only one hash per document" in combination with the impression that
sammy is very "hash-centric" makes me uncertain..

i am still looking for the best approach/framework to develop
javascript-based-singlepage-webapps/RIAs in terms of simplicity/
flexibility...
as i do not have enough time to evaluate all of these heavyweight-
approaches - like extjs3/sproutcore/yui3/javascriptmvc/cappuccino or
whatsoever - for the moment, jQ/sammy is the way i go..

so i am very excited about things like proxys :)

cheers,
dpree


On 18 Dez., 21:42, Aaron Quint <aa...@quirkey.com> wrote:
> Well the one thing about having multiple apps, is that if you're using
> the hash (#) to track state, both apps have to agree on being in the
> same state at the same time as theres only one hash per document.
> However, you can maitain the state in an internal variable, the
> benefit being you can have multiple apps in multiple states on a
> single page. This isnt super super easy now, but Sammy 0.4 (coming
> soon) will support creating proxys that define how state is
> maintained.
>
> --AQ
>

> Aaron Quinthttp://www.quirkey.com

Richard Bucker

unread,
Apr 23, 2013, 11:36:32 AM4/23/13
to sam...@googlegroups.com, aa...@quirkey.com
this works most of the time but it does not always fire.
Reply all
Reply to author
Forward
0 new messages