淘汰js很多自定义绑定 [英] Knockout js lots of custom bindings

查看:58
本文介绍了淘汰js很多自定义绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不存在绑定的情况下,如何处理大量自定义绑定的好方法是什么? 说我的html表达式绑定到image_url如下.

<span title="Company Logo" data-bind="image_url: company_banner"></span>

但是,很可能image_url绑定不可用. 在这种情况下,我只想返回一个字符串值company_banner.

通常情况下,我们会像下面这样添加一个自定义处理程序,但是如果该处理程序不可用,我们可以返回一些通用反馈吗?

ko.bindingHandlers.buttonLabel = {//update etc}

在我们的情况下,设计可能在代码之前,所以我们不希望ko抱怨.

解决方案

在这种情况下,我将考虑使用自定义绑定提供程序.以下是描述此功能的文章: http ://www.knockmeout.net/2011/09/ko-13-preview-part-2-custom-binding.html .

因此,我将创建一个自定义绑定提供程序,该提供程序是真实绑定提供程序的包装.解析了绑定之后,我们可以检查它们以查看它们是否存在于ko.bindingHandlers中.如果没有,那么我们可以添加一个带有其值的文本绑定.

它可能看起来像:

ko.lenientBindingProvider = function() {
   var realBindingProvider = new ko.bindingProvider();

   this.nodeHasBindings = realBindingProvider.nodeHasBindings;

   this.getBindings = function(node, bindingContext) {
       //parse the bindings with the real binding provider
       var result = realBindingProvider.getBindings(node, bindingContext);

       //inspect the returned bindings
       for (var binding in result) {
           if (result.hasOwnProperty(binding) && binding !== "_ko_property_writers" && !ko.bindingHandlers[binding]) {
                //add a text binding with whatever the missing binding was bound against
                result.text = result[binding];
           } 
       }

       return result;  
   };
};

ko.bindingProvider.instance = new ko.lenientBindingProvider();

这里是一个示例: http://jsfiddle.net/rniemeyer/mMQKY/

What would be considered a good way of handling lots of custom bindings with the possibility that the binding is not present? Say my html expression binds to image_url as below.

<span title="Company Logo" data-bind="image_url: company_banner"></span>

However there is every possibility that the image_url binding is not available. In that case scenario, I'd just like to return a string value of company_banner.

Normally one would add a custom handler like below but if that handler is not available can we return some generic feedback?

ko.bindingHandlers.buttonLabel = {//update etc}

In our case the design may be ahead of the code, so we don't want ko to grumble.

解决方案

For this scenario, I would look at using a custom binding provider. Here is an article describing the functionality: http://www.knockmeout.net/2011/09/ko-13-preview-part-2-custom-binding.html.

So, I would create a custom binding provider that is a wrapper to the real binding provider. Once the bindings are parsed, then we can inspect them to see if they exist in ko.bindingHandlers. If one does not, then we can add a text binding with its value.

It might look like:

ko.lenientBindingProvider = function() {
   var realBindingProvider = new ko.bindingProvider();

   this.nodeHasBindings = realBindingProvider.nodeHasBindings;

   this.getBindings = function(node, bindingContext) {
       //parse the bindings with the real binding provider
       var result = realBindingProvider.getBindings(node, bindingContext);

       //inspect the returned bindings
       for (var binding in result) {
           if (result.hasOwnProperty(binding) && binding !== "_ko_property_writers" && !ko.bindingHandlers[binding]) {
                //add a text binding with whatever the missing binding was bound against
                result.text = result[binding];
           } 
       }

       return result;  
   };
};

ko.bindingProvider.instance = new ko.lenientBindingProvider();

Here is a sample: http://jsfiddle.net/rniemeyer/mMQKY/

这篇关于淘汰js很多自定义绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆