knockout js basic technology

1. how to push all list into observal array

self.inverters = ko.observableArray([]);
self.loadInverters = function(inverters) {
	var array = self.inverters();
	ko.utils.arrayPushAll(array, inverters);
	self.inverters.valueHasMutated();
};

 2. filter list for objserval array

function homeViewModel() {
	var self = this;
	self.teams = ko.observableArray([]);
	self.filterText = ko.observable("");
	self.filteredTeams = ko.computed(function() {
		if (self.filterText().length > 0) {
			var teamsArray = self.teams();			
			return ko.utils.arrayFilter(teamsArray, function(team) {
				 return ko.utils.stringStartsWith(team.location.toLowerCase(), self.filterText())
			});
		}
	});
}

3. multiple bind

<a data-bind="html: name, attr: { href: url }">

 4.  bind to simple array of strings

When using a template: ${$data}

When not using a template: $data

<ul data-bind="foreach: $root">
 <li data-bind="text: $data"></li>
</ul>

  $root keyword does the trick.

or

<ul data-bind='foreach: list'>
    <li>
        <input data-bind='value: $parent.list[$index()]' />
    </li>
</ul>

猜你喜欢

转载自sunxboy.iteye.com/blog/2161182