1 // "foreach: someExpression" is equivalent to "template: { foreach: someExpression }"
2 // "foreach: { data: someExpression, afterAdd: myfn }" is equivalent to "template: { foreach: someExpression, afterAdd: myfn }"
3 ko.bindingHandlers['foreach'] = {
4 makeTemplateValueAccessor: function(valueAccessor) {
6 var modelValue = valueAccessor(),
7 unwrappedValue = ko.utils.peekObservable(modelValue); // Unwrap without setting a dependency here
9 // If unwrappedValue is the array, pass in the wrapped value on its own
10 // The value will be unwrapped and tracked within the template binding
11 // (See https://github.com/SteveSanderson/knockout/issues/523)
12 if ((!unwrappedValue) || typeof unwrappedValue.length == "number")
13 return { 'foreach': modelValue, 'templateEngine': ko.nativeTemplateEngine.instance };
15 // If unwrappedValue.data is the array, preserve all relevant options and unwrap again value so we get updates
16 ko.utils.unwrapObservable(modelValue);
18 'foreach': unwrappedValue['data'],
19 'as': unwrappedValue['as'],
20 'includeDestroyed': unwrappedValue['includeDestroyed'],
21 'afterAdd': unwrappedValue['afterAdd'],
22 'beforeRemove': unwrappedValue['beforeRemove'],
23 'afterRender': unwrappedValue['afterRender'],
24 'beforeMove': unwrappedValue['beforeMove'],
25 'afterMove': unwrappedValue['afterMove'],
26 'templateEngine': ko.nativeTemplateEngine.instance
30 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
31 return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor));
33 'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
34 return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
37 ko.expressionRewriting.bindingRewriteValidators['foreach'] = false; // Can't rewrite control flow bindings
38 ko.virtualElements.allowedBindings['foreach'] = true;