Native event-based jQuery + Sammy.js

271 views
Skip to first unread message

Jeff M

unread,
Apr 22, 2013, 5:25:10 PM4/22/13
to sam...@googlegroups.com
Hello,

I'm fairly new to Sammy.js, so forgive me if this is somewhat of a stupid question.

I'm using Sammy.js + mustache.js for all of my routing and templating needs and it's working swimmingly. However, some of my sites "juice" if you will, requires event-based jQuery (calls to "$('.class').clicked()", for example, using "this.attr('id')" within to find the element id to work with), and I'm having a hell of a time getting this code to mix with Sammy. If I try to include it outside of Sammy in it's own library, it won't fire any of the events and I can't seem to find where in Sammy you would add this code (.then just seems to launch the code inside the events, regardless of whether they're supposed to be triggered or not).

Again, this is likely a stupid question with an easy solution, but I haven't been able to find anything with all my trawling through the doc's.

Thanks in advance,
Jeff

Jeff M

unread,
Apr 23, 2013, 4:54:02 AM4/23/13
to sam...@googlegroups.com
Just to help clarify, I have a boilerplate html as follows:

=================
<!DOCTYPE html>

<html>

<head>

<title>Feed</title>

<meta charset="utf-8"">

<link rel="stylesheet" type="text/css" href="/static/css/main.css">

</head>

<div id="viewPort">

<div id="lister"></div>

<div id="mainContent"></div>

</div>

<script id="channelTemplate" type="text/template">

<div id="channelHeadline">

<div id="channelTitle">{{title}}</div>

<div id="channelOptions"></div>

</div>

<div id="channelListing"></div>

</script>

<script id="channelItemTemplate" type="text/template">

<div id="fi{{id}}" class="feedItem collapsed unread">

<div class="fiTitle">

<span class="date">{{date}}</span>

<div class="fiContent">

<span class="fiStarred">*</span>

<span class="title">{{title}}</span>

<span class="desc"> - {{desc}} </span>

</div>

</div>

<div class="fiData">

<h1><a href="{{item.source_link}}" target="_blank">{{title}}</a></h1>

{{summary}}

</div>

</div>

</script>

<script src="/static/js/jquery.js"></script>

<script src="/static/js/sammy.js"></script>

<script src="/static/js/mustache.js"></script>

<script src="/static/js/feedJuice.js"></script>

<script src="/static/js/feed.js"></script>

</html>

=================

And then, my app (feed.js):

=================

(function($) {

    var feedishRouter = $.sammy('#mainContent',  function() {

        this.get('#/channel/:id', function(context) {

            that = this;

            context.$element().html(Mustache.to_html($('#channelTemplate').html()));

            

            var clTpl = $('#channelItemTemplate').html();

            

            $.get('/channelContent/' + that.params['id'], function(data) {

                $.each($.parseJSON(data), function(index, currentData) {

                    var listItemRendered = Mustache.to_html(clTpl, currentData);

                    $('#channelListing').append(listItemRendered);

                });

            });

        });

        

        this.get('#/search/:query', function(context) {

            context.$element().html('searching for <b>' + this.params['query'] +'</b>');

        });

    });

    

    $(function() {

        feedishRouter.run('#/explore');

    });

} (jQuery));

=================

I'm wondering where to place jquery event-based code that would trigger at anytime. E.g. a click on an item with the "fiTitle" class. So something like:

=================

$('.fiTitle').click(function() {

    var $currentID = '#' + $($(this).parent()).attr('id');

        

    if($($currentID).hasClass('collapsed')) {

        $($currentID + ' .fiData').show();

        $($currentID).removeClass('collapsed');

        $($currentID).addClass('expanded');

        $($currentID).removeClass('unread');

    } else if($($currentID).hasClass('expanded')) {

        $($currentID + ' .fiData').hide();

        $($currentID).removeClass('expanded');

        $($currentID).addClass('collapsed');

    }

});

=================

I appreciate the assistance,

Jeff M

Vishal Rajpurohit

unread,
Apr 23, 2013, 4:58:39 AM4/23/13
to sam...@googlegroups.com
s
​ure.. not able to fix it in quik time brother... loaded with some critical work now... will look on it and then update you... 

where are you from? im from india :) also added u in gtalk... ​


-- 
Regards,


Vishal Rajpurohit
Viitorcloud Technologies Pvt. Ltd.


Confidential Information and Disclaimer:

This communication sent from "Viitorcloud Technologies Private Limited" is confidential and intended solely for the use of addresses. Any retransmission, dissemination or the use of this information by persons other than addressee is unlawful and prohibited. The views expressed here-in (including any attachments) are those of the individual sender only. We do not accept any liability what-so-ever including on account of any errors, omissions, viruses etc in the contents of this message.



--
You received this message because you are subscribed to the Google Groups "Sammy.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sammyjs+u...@googlegroups.com.
To post to this group, send email to sam...@googlegroups.com.
Visit this group at http://groups.google.com/group/sammyjs?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Andriy Zhdanov

unread,
Apr 24, 2013, 4:15:16 AM4/24/13
to sam...@googlegroups.com
Hi Jeff,

I've been asking same question some time ago, looks like there is no right answer:


Thank you.

mohammad raza

unread,
Mar 29, 2014, 3:08:49 PM3/29/14
to sam...@googlegroups.com
Hello, Jeff, i have the same problem. i tried everything, but still no solution found. if u find any solution, please let me know...
Raza 

Rikki Prince

unread,
Aug 15, 2014, 11:57:19 AM8/15/14
to sam...@googlegroups.com
I can't work this out either. I'm a bit astounded that a framework built on top of jQuery didn't consider how to let people use regular jQuery features within it!

I thought the problem might be that the DOM isn't ready by the time I tried to attach events. Try logging the result of doing a jQuery selection (e.g. $('div')) and see what you get.

Anyone got any idea why it isn't working? I'm happy to attempt a patch, but I'm not sure what's causing the problem! (To the debugger, I guess :-)

Cheers,
Rikki

Rikki Prince

unread,
Aug 17, 2014, 9:50:01 PM8/17/14
to sam...@googlegroups.com
Figured it out, at least for my scenario!

Using .then() gets you an updated view of the DOM. Without it, I think the jQuery line runs before the rendering has finished, so finds nothing.

My problem was I was using $.each() to loop through the items in my .around()-loaded array, as advised in the tutorial, but you cannot attach a .then() to a $.each()!

I ended up figuring out how to use .renderEach() properly, instead of $.each(), after working out how it works and submitting a pull request to improve the documentation.

I blogged more about the details, in case you're interested:

Cheers,
Rikki
Reply all
Reply to author
Forward
0 new messages