migrated knockout asset to bower
[openemr.git] / public / assets / knockout-2-2-1 / src / binding / defaultBindings / foreach.js
blob7a05eb9b243bc55545a5f35ed8ddb1235b2b31ce
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) {
5         return function() {
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);
17             return {
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
27             };
28         };
29     },
30     'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
31         return ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor));
32     },
33     'update': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
34         return ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor), allBindingsAccessor, viewModel, bindingContext);
35     }
37 ko.expressionRewriting.bindingRewriteValidators['foreach'] = false; // Can't rewrite control flow bindings
38 ko.virtualElements.allowedBindings['foreach'] = true;