Here is an example binding for the union of all programming languages.
<h4>Laguages</h4>
<ul data-bind="foreach: languages">
<li data-bind="text: $data"></li>
</ul>
And here is the model, with several programming language arrays. The languages property is a computed observable where we're using _.sortBy() and _.union() to return an array of all unique programming language names.
function LanguageModel() {
this.objectOriented = ko.observableArray([
"C++",
"Java",
"Lua",
"PHP",
"Python",
"Ruby",
"Smalltalk"
]);
this.procedural = ko.observableArray([
"Ada",
"C",
"C++",
"COBOL",
"JavaScript",
"Go",
"Java",
"Python"
]);
this.reflective = ko.observableArray([
"C#",
"Java",
"Lisp",
"Lua",
"Prolog"
]);
this.scripting = ko.observableArray([
"AWK",
"Bash",
"Groovy",
"Python"
]);
this.languages = ko.computed(function() {
return _.sortBy( _.union(
this.objectOriented(),
this.procedural(),
this.reflective(),
this.scripting()
));
}, this );
};
ko.applyBindings( new LanguageModel() );
No comments :
Post a Comment