80
rated 0 times
[
81]
[
1]
/ answers: 1 / hits: 23682
/ 12 Years ago, mon, february 18, 2013, 12:00:00
I have the code duplication problem in the next case. On my page I have a lot of blocks that I need to show / hide by clicking to link:
<div>
<a data-bind=click: showHiddenFirst, visible: isVisibleFirsthref=#>Show first</a>
<div data-bind=visible: !isVisibleFirst() style=display:none>
hidden content first
</div>
</div>
<div>
<a data-bind=click: showHiddenSecond, visible: isVisibleSecondhref=#>Show second</a>
<div data-bind=visible: !isVisibleSecond() style=display:none>
hidden content second
</div>
</div>
And my JS
var vm = function(){
this.isVisibleFirst = ko.observable(true);
this.showHiddenFirst = function(){
this.isVisibleFirst(false)
};
this.isVisibleSecond = ko.observable(true);
this.showHiddenSecond = function(){
this.isVisibleSecond(false)
};
};
ko.applyBindings(new vm());
Here is jsfiddle example http://jsfiddle.net/sstude/brCT9/2/
Question is how to avoid all this show / visible duplication? Maybe I need some custom binding where I put id of my hidden block or smth. else? Any patterns that you can suggest?
More From » design-patterns