/**
*    @Author: Arash Karimzadeh
*    @Email:
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 334745
Bookmark and Share
jBind (jQuery Bind Template) 1.0.0-beta PDF Print
Written by Arash Karimzadeh   
Saturday, 17 January 2009 17:57

jBind is jQuery plugin for binding any complex data to templates in an easy way. By using $(template).bind(data) you can bind your data to the template.

This Plugin is Updated, please check here.

Download jBind (jQuery Bind Template)

This is jBind 1.0.0 beta
You can get it here.

Or download the latest version here.

jBind

This Plugin is Updated, please check here.

  1. Format
  2. Options
  3. How to map data to template?
  4. Examples

Format

$(template).bind(data,[options]);

  • in jBind version 1.5.0 the bind has changed to bindTo.
  • template is a string of the template you want the data to be bound to it.
  • data is complex data type such as {id:1, name:'Scott', family:'Adams', birthdate: 'June 8 1957'}, or can be array of objects or even nested object such as [{item:value,item:value,item:[{item:value,item:value},{item:value,item:value},..],item:{item:value,...}},....]

Options

  • root [string]

[default = 'data']
The root node (in template). jBind use it to start traversing the template.

  • appendTo [string]

[default = null]
If you do not set this option the bind method will return the content.Otherwise it will append the content to the element(s) and return the jQuery object created content.

  • onBind [function]

You can define a function to be called at the beginning of bind action.
  • onBound [function]

You can define a function to be called at the end of bind action. It accepts content, data as inputs.


              
  1. function onBound(content,data){
  2. //data is the passed data
  3. //content is the replaced template (if appendTo is not defined) or jQuery object
  4. }

              
 

              
            

How to map data to template?


  • Template is a string of html tags or can be inner html of an element which is returned by $(selector).html()
  • Data must be options or array of options.
    1. {name:'John',age:25,education:'IT'}
      Each key will be replaced in template by a placeholder
      for example name will be replaced with {name}
    2. [{name:'John',age:25,education:'IT'},{name:'John',age:25,education:'IT'},....]
      When you use array it means a repetitive action so you must use
      In case it is an array inside an option such as below, you must replace the name with options key
      {name:'John',roles:[{role:'author',type:2},{role:'owner',type:3}]}
      So it would be , each repeater must have an end tag which is the same as
    3. Finally you must place your tags inside
  • When jBind find .... ,it will understand that all the elements {name} inside these tags must be replaced with a parameter of provided data, or it will check to find if it is an array?So it will use the content of .... as template for each item inside the array and repeat it.
  • Lets create the template of previous example:
    {name:'John',roles:[{role:'author',type:2},{role:'owner',type:3}]}
    As you see roles is array of roles so the template for it must be like this:
    
                      
    1. {role}, {type}
    
                      
     
    
                      
                    

    Also roles and name are in options object so we have some thing like this.
    
                      
    1. {name}
    2. {role}, {type}
    
                      
     
    
                      
                    

    Because of jBind rule number 2 we must place the template in tag
    
                      
    1. {name}
    2. {role}, {type}
    
                      
     
    
                      
                    
    This Template also accept this:
    [{name:'John',roles:[{role:'author',type:2},{role:'owner',type:3}]},
    {name:'Jack',roles:[{role:'author',type:2},{role:'reader',type:4}]},
    ...]
You can see more examples below or can download the samples at the bottom of page and customize them.

Examples (Bind Data to Template)

  1. Bind a hash to a template
    
                      
    1. var data = {id:1,
    2. name:'Scott',
    3. family:'Adams',
    4. birthdate: 'June 8 1957'
    5. };
    6. $(template).bind(data,{'root':'info',appendTo:'.placeholder'});
    
                      
     
    
                      
                    

    and the template is:
    
                      
    1. class="content">
    2. class="viewBlock" id='{id}'>
    3. {name} {family}, [ Birthdate: {birthdate} ]

                  
 

                  
                

*In these example don't forget to add jQuery ready function to each code. so your code must be like this


                  
  1. $(function(){
  2. var data = {id:1,
  3. name:'Scott',
  4. family:'Adams',
  5. birthdate: 'June 8 1957'
  6. };
  7. $(template).bind(data,{'root':'info',appendTo:'.placeholder'});
  8. });

                  
 

                  
                

  • You can grab template from html element
    
                      
    1. var template = $('#template2').html();
    2. var data = [
    3. {
    4. id:41,
    5. name:'Scott',
    6. family:'Adams',
    7. education:'Economics'
    8. },
    9. {
    10. id:59,
    11. name:'Jack',
    12. family:'Welch',
    13. education:'Chemical Engineering'
    14. }
    15. ];
    16. alert($(template).bind(data));
    
                      
     
    
                      
                    

    the #template2 is something like this
    
                      
    1. id='template2' style="display:none;">
    2. class="content">
    3. class="viewBlockLeft" id='{id}'>
    4. #{id}/>
    5. {name} {family},/>
    6. {education},/>
  • class="clear">
  • 
                      
     
    
                      
                    
                  
                  
  • This is the most complex one
    
                      
    1. var data = [
    2. {
    3. id:41,
    4. name:'Scott',
    5. family:'Adams',
    6. education:'Economics',
    7. history:"Scott Adams was born in Windham, New York in 1957 and received ...",
    8. birthdate: {month:'June',date:8,year:1957},
    9. Publications: [{book:'The Dilbert Future',year:1997},{book:'The Dilbert Principle',year:1996}]
    10. },
    11. {
    12. id:59,
    13. name:'Jack',
    14. family:'Welch',
    15. education:'Chemical Engineering',
    16. history:'Jack Welch was born in Peabody, Massachusetts to John, ...',
    17. birthdate: {month:'November',date:19,year:1935},
    18. Publications: [{book:'Winning',year:2005}]
    19. }
    20. ];
    21. var node = $(template).bind(data);
    22. $(node).appendTo('#show');
    
                      
     
    
                      
                    
    
                      
    1. class="content">
    2. class="viewBlock" id='{id}'>
    3. #{id}/>
    4. {name} {family},/>
    5. {education},/>
    6. {history}
    7. class='age'>
    8. {month} {date} {year}
  • class='role'>
  • {book} ({year})
  • 
                      
     
    
                      
                    
                  
  • You can download all the jBind examples as zip file here.

    *Currently it is beta version please send your feedbacks to This e-mail address is being protected from spambots. You need JavaScript enabled to view it so I could upgrade it with new features.

    Tags: jBind | jQuery | Plugin

    Last Updated on Monday, 27 September 2010 12:44