Unique ids in knockout.js templates

后端 未结 3 1079
天命终不由人
天命终不由人 2020-11-30 02:22

Suppose I have knockout.js template like this:



        
3条回答
  •  佛祖请我去吃肉
    2020-11-30 02:46

    An alternative that does not rely on the order that the fields are bound is to have the binding set an id property on the data itself, which would need to be an observable.

    ko.bindingHandlers.uniqueId = {
        init: function(element, valueAccessor) {
            var value = valueAccessor();
            value.id = value.id || ko.bindingHandlers.uniqueId.prefix + (++ko.bindingHandlers.uniqueId.counter);
    
            element.id = value.id;
        },
        counter: 0,
        prefix: "unique"
    };
    
    ko.bindingHandlers.uniqueFor = {
        init: function(element, valueAccessor) {
            var value = valueAccessor();
            value.id = value.id || ko.bindingHandlers.uniqueId.prefix + (++ko.bindingHandlers.uniqueId.counter);
    
            element.setAttribute("for", value.id);
        } 
    };
    

    You would use it like:

    Sample: http://jsfiddle.net/rniemeyer/JjBhY/

    The nice thing about adding a property to the observable function is that when you turn it into JSON to send back to the server, then it will just naturally disappear as the observable will just turn into its unwrapped value.

提交回复
热议问题