Knockout>The "template" binding

Knockout的模板绑定,

模版可以用来方便构建复杂的HTML界面,比如拥有嵌套结构的HTML代码页面。

Knockout有两种方式使用模板:

1.Knockout自带的模板引擎。

2.第三方的模版引擎,比如jQuery.tmpl

Knockout自带模版引擎的参数列表:

data-bind= "template: { name: 'person-template', data/foreach: buyer,afterRender/afterAdd/beforeRemove}"

name参数就是所对应你定义的模板id,

data参数就是你的模板数据源,指向一个js对象;foreach参数也是,指向一个js对象数组。

 

afterRender/afterAdd/beforeRemove这三个,就是事件咯,触发时机看名字,它们回调函数。

 

 

第一个例子, 没什么好说

<! DOCTYPE html >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
< title >The "template" binding </ title >
< script  type ="text/javascript"  src ="../jquery-1.7.1.js" ></ script >
< script  type ="text/javascript"  src ="../knockout-2.0.0.debug.js" ></ script >
< script  type ="text/javascript" >
$(
function (){

    
function  MyViewModel() {
        
this .buyer  =  { name:  ' Franklin ' , credits:  250  };
        
this .seller  =  { name:  ' Mario ' , credits:  5800  };    
    }
    ko.applyBindings(
new  MyViewModel());     

})

</ script >
</ head >
< body >
< h2 >Participants </ h2 >
Here are the participants:
< div  data-bind ="template: { name: 'person-template', data: buyer }" ></ div >
< div  data-bind ="template: { name: 'person-template', data: seller }" ></ div >
< script  type ="text/html"  id ="person-template" >
    
< h3 data - bind = " text: name " >< / h3>
     < p > Credits:  < span data - bind = " text: credits " >< / span>< / p >
</ script >
</ body >
</ html >

 

 

第二个例子:解释就是,第一个例子用了data参数,第二个例子用了foreach参数

<! DOCTYPE html >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
< title >The "template" binding </ title >
< script  type ="text/javascript"  src ="../jquery-1.7.1.js" ></ script >
< script  type ="text/javascript"  src ="../knockout-2.0.0.debug.js" ></ script >
< script  type ="text/javascript" >
$(
function (){
    
function  MyViewModel() {
        
this .people  =  [
            { name: 
' Franklin ' , credits:  250  },
            { name: 
' Mario ' , credits:  5800  }
        ]
    }
    ko.applyBindings(
new  MyViewModel());

})

</ script >
</ head >
< body >
< h2 >Participants </ h2 >
Here are the participants:
< div  data-bind ="template: { name: 'person-template', foreach: people }" ></ div > 
< script  type ="text/html"  id ="person-template" >
    
< h3 data - bind = " text: name " >< / h3>
     < p > Credits:  < span data - bind = " text: credits " >< / span>< / p >
</ script >
</ body >
</ html >

 

第三个例子:动态选择模板,往name里面传入一个方法,用来筛选用哪个模板

<! DOCTYPE html >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
< title >The "template" binding </ title >
< script  type ="text/javascript"  src ="../jquery-1.7.1.js" ></ script >
< script  type ="text/javascript"  src ="../knockout-2.0.0.debug.js" ></ script >
< script  type ="text/javascript" >
$(
function (){
    
var  viewModel  =  {
        employees: ko.observableArray([
            { name: 
" Kari " , active: ko.observable( true ) },
            { name: 
" Brynn " , active: ko.observable( false ) },
            { name: 
" Nora " , active: ko.observable( false ) }
        ]),
        displayMode: 
function (employee) {
            
return  employee.active()  ?   " active "  :  " inactive " ;
        }
    };
    
// viewModel.employees()[1].active(true);
    ko.applyBindings(viewModel);

})

</ script >
</ head >
< body >
< h2 >Participants </ h2 >
Here are the participants:
< div  data-bind ="template: { name: displayMode, foreach: employees }" ></ div > 
< script  type ="text/html"  id ="active" >
    active:
< span data - bind = " text: name " >< / span>
</ script >
< script  type ="text/html"  id ="inactive" >
    inactive:
< span data - bind = " text: name " >< / span>
</ script >
</ body >
</ html >

 

至于如何用第三方模板引擎,比如jQuery.tmpl就不说了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值