/**
*    @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 : 338034
Bookmark and Share
jBind (jQuery Template) Latest Version PDF Print
Written by Arash Karimzadeh   
Sunday, 25 January 2009 00:00

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

In version 1.5.3 actions are enabled which means you can also treat special item in your data differently by extending $.fn.bindTo of this templating engine with your own functionality and apply it during binding.

Download jBind (jQuery Bind Template)

Current version is 1.5.8
You can get it here.

jBind

  1. Formats
  2. Options
  3. How to map data to template?
  4. How to implement function inside template?
  5. Examples & Downloads

Formats

$(template).bindTo(data,[options]);
$(selector).bindTo(data,[options]);

  • template is a string of the template you want the data to be bound to it.
  • selector [version 1.5.3] is used when you set fill option to true, you must use css selector. download the examples and check the fill.html for more information or check jBind options.
  • 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.

  • fill [boolean] added in jBind 1.5.3

[default = false]
When you want to fill a template once and show it set this property to true. For more information on this option check here.

  • 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.

How to implement function inside template?

With jBind you can add define action inside the template:

  1. Add a html comment tag withthis format which you can change the functionName to whatever you want.
  2. extend the $.fn.bindTo with the same functionName you have chosen.

For example your template is:


              
  1. class="content">
  2. class="viewBlock {cl}" id='{id}'>
  3. #{id}/>
  4. {name} {family},/>
  5. {education},/>
  6. class='age'>
  7. {month} {date} {year}
  • 
                  
     
    
                  
                
                

    And Your code would be like this:

    
                  
    1. $.fn.bindTo.colorized = function(template,data){
    2. if(data.id>50)
    3. template = template.replace('{cl}','extraClass');
    4. return template;
    5. }
    6. $(template).bindTo(data,{'root':'info',appendTo:'.placeholder'});
    
                  
     
    
                  
                

    This code will only affects on items bounded in ... . You can download the sample codes which I provides at the end of this page.

    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).bindTo(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).bindTo(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).bindTo(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).bindTo(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.

    *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:43