Multi level navigation using backbone.js

Making multi level nested ul li navigation is not much difficult, Heres the code below to make an nested navigation.

<!DOCTYPE html>
<html>
<head>
    <title>Backbone</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://underscorejs.org/underscore.js"></script>
    <script type="text/javascript" src="http://backbonejs.org/backbone.js"></script>

    <script type="text/template" id="parent-template">
        <ul id='ul-<%= parent_id %>'></ul>
    </script>
    <script type="text/template" id="child-template">
        <li id="li-<%= id %>"><%= name %></li>
    </script>

    <script type="text/javascript">

    var compiledParent = _.template( $('#parent-template').html() );
    var compiledChild = _.template( $('#child-template').html() );

    var data = [
        {"id":1,"name":"one","parent_id":0},
        {"id":2,"name":"two","parent_id":0},
        {"id":3,"name":"three","parent_id":0},
        {"id":4,"name":"four","parent_id":0},
        {"id":5,"name":"five","parent_id":0},
        {"id":6,"name":"one","parent_id":2},
        {"id":7,"name":"two","parent_id":2},
        {"id":8,"name":"three","parent_id":2},
        {"id":9,"name":"four","parent_id":2},
        {"id":10,"name":"five","parent_id":2},
    ];

    var NestedView = Backbone.View.extend({
      render: function(){
        this.$el.html( compiledParent({parent_id : '0'}) );
        this.collection.each( function( model ){ this.renderElement( model ) }, this )
      },

      renderElement: function( model ){
        var ul = this.getParentUl( model );
        this.appendElement( ul, model );
      },

      getParentUl: function( model ) {
        var ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
        if( ul.length == 0 ) {
          this.appendListInElement( model );
          ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
        }

        return ul;
      },

      appendListInElement: function( model ){
        var li = this.$el.find( "#li-" + model.get( "parent_id" ) );
        li.after( compiledParent({parent_id : model.get( "parent_id" )}) );
      },

      appendElement: function( ul, model ){
        ul.append( compiledChild({id:model.get( "id" ) , name:model.get( "name" )}) );
      }
    });

    $(document).ready(function()
    {
        var elements = new Backbone.Collection( data );
        var nestedView = new NestedView({ el: "#wrapper", collection: elements });
        nestedView.render();
    });
    </script>
</head>
<body>
    <div id="wrapper">
    </div>
</body>
</html>
Advertisements

Facebook application’s iFrame to auto resize

If you have a Facebook iFrame application and you want to auto resize the ifram and have┬áread Facebook’s horrible documentation for their js sdk and have tried so many different variations – all of them not working at all. This code will work for you!

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
window.fbAsyncInit = function()
{
    FB.Canvas.setSize();
}
</script>
</head>
<body>

    <!-- Some of your html code -->
    <div id="fb-root"></div>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script> 
    <script type="text/javascript">

    FB.init({
        appId: 'xxxxxxxx', 
        status: true, 
        cookie: true, 
        xfbml: true
    });

    /* Init fb Resize window */

    /* Completly removed now in JS SDK*/
    /* FB.Canvas.setAutoResize(7); */

    /* As of Jan 2012 you need to use */
    FB.Canvas.setAutoGrow(7);

    </script>
</body>
</html>