knockoutjs(02) ko & jsrender

<ul data-bind="template: 'peopleList'"></ul>

<script type="text/html" id="peopleList">
    {{for people()}}
        <li>
            {{: first() }}
        </li>
     {{/for}}
</script>

<hr/>

<ul data-bind="template: { name: 'peopleList2', foreach: people }"></ul>

<script type="text/html" id="peopleList2">
        <li>
            {{: first() }}
        </li>
</script>

<hr/>

<ul data-bind="template: { name: 'peopleList3', foreach: people }"></ul>

<script type="text/html" id="peopleList3">
        <li>
            <input data-bind="value: first" />
            <a href="#" data-bind="click: $root.removePerson"> x </a>
        </li>
</script>

<hr/>
            
<button data-bind="click: addPerson">Add</button>



        
//jsRender engine starting here   
(function(ko, jsviews, $) {
    if (jsviews || $.views) {
        ko.jsRenderEngine = function() {
            //if no jQuery, then need to use jsviews
            var compiler = jsviews ? jsviews.templates : $.templates,
                views = jsviews ? jsviews : $.views;

            //save off the compiled template and render
            this.renderTemplateSource = function(templateSource, bindingContext) {
                var compiled = templateSource.data("compiled");
                if (!compiled) {
                    compiled = compiler(templateSource.text() || "");
                    templateSource.data("compiled", compiled);
                }

                return ko.utils.parseHtmlFragment(compiled.render(bindingContext.$data, { koBindingContext: bindingContext }));
            };
            
            //ko adds code to hook up bindings after rendering
            this.createJavaScriptEvaluatorBlock = function(script) {
                return "{{:~ko_with(\"" + script + "\") }}";
            };
            
            //ko expects to be able to find the variables directly            
            views.helpers({
               ko_with: function(script) {
                   var wrapped = "with(context) { with(data) { return " + script + "} }";
                   return (new Function("context", "data", wrapped))(this.ctx.koBindingContext, this.data);         
               }
            });
        };
        
        ko.jsRenderEngine.prototype = new ko.templateEngine();

        //set the default engine to be the jsRender engine
        ko.setTemplateEngine(new ko.jsRenderEngine());
    }
})(ko, this.jsviews, this.jQuery);
//end jsRender engine


//View Model code
var Person = function(first, last, age) {
   this.first = ko.observable(first);
   this.last = ko.observable(last);
   this.age = ko.observable(age);   
};

var ViewModel = function() {
    var self = this;
    
    this.message = ko.observable("hello");
    
    this.people = ko.observableArray([
        new Person("Bob", "Smith", 30),
        new Person("Jim", "Jones", 22),
        new Person("Sally", "Johnson", 41)       
    ]);
    
    this.addPerson = function() {
        self.people.push(new Person("new", "person", 0));  
    };
                         
    this.removePerson = function(person) {
        self.people.remove(person);    
    };
};
        
ko.applyBindings(new ViewModel());

http://jsfiddle.net/rniemeyer/MvCjB/

转载于:https://www.cnblogs.com/rock_chen/archive/2012/10/17/2727671.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值