kea-rails 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/kea/bindings/activity.js +24 -0
  6. data/app/assets/javascripts/kea/bindings/child_vm.js +32 -0
  7. data/app/assets/javascripts/kea/bindings/komplete.js +172 -0
  8. data/app/assets/javascripts/kea/bindings/sherlock.js +47 -0
  9. data/app/assets/javascripts/kea/bindings/sherlock_provider_search.js +57 -0
  10. data/app/assets/javascripts/kea/bindings/submit_button.js +33 -0
  11. data/app/assets/javascripts/kea/bindings/validation_state.js +38 -0
  12. data/app/assets/javascripts/kea/bindings/wait_for_vm.js +44 -0
  13. data/app/assets/javascripts/kea/extenders/equals.js +85 -0
  14. data/app/assets/javascripts/kea/extenders/external_validator.js +65 -0
  15. data/app/assets/javascripts/kea/extenders/min_length.js +39 -0
  16. data/app/assets/javascripts/kea/extenders/numeric_range.js +64 -0
  17. data/app/assets/javascripts/kea/extenders/required.js +39 -0
  18. data/app/assets/javascripts/kea/extenders/valid_date.js +32 -0
  19. data/app/assets/javascripts/kea/extenders/valid_time.js +41 -0
  20. data/app/assets/javascripts/kea/extenders/validator_base.js +75 -0
  21. data/app/assets/javascripts/kea/helpers/kea.activity_button.js +97 -0
  22. data/app/assets/javascripts/kea/helpers/kea.notify.js +46 -0
  23. data/app/assets/javascripts/kea/initializers/base.js +11 -0
  24. data/app/assets/javascripts/kea/kea.js +14 -0
  25. data/app/assets/javascripts/kea/kea_dependencies.js +20 -0
  26. data/app/assets/javascripts/kea/kea_init.js +24 -0
  27. data/app/assets/javascripts/kea/models/base.js +215 -0
  28. data/app/assets/javascripts/kea/overlay/child_vm_overlay.js +49 -0
  29. data/app/assets/javascripts/kea/overlay/overlay_control.js +87 -0
  30. data/app/assets/javascripts/kea/overlay/overlay_template.js +58 -0
  31. data/app/assets/javascripts/kea/services/base.js +227 -0
  32. data/app/assets/javascripts/kea/sherlock/base_provider.js +201 -0
  33. data/app/assets/javascripts/kea/viewmodels/base.js +150 -0
  34. data/app/assets/javascripts/kea/viewmodels/sherlock.js +230 -0
  35. data/app/assets/stylesheets/kea/kea.css.sass +16 -0
  36. data/app/helpers/kea/application_helper.rb +42 -0
  37. data/lib/generators/kea/install/USAGE +9 -0
  38. data/lib/generators/kea/install/install_generator.rb +197 -0
  39. data/lib/generators/kea/install/templates/_komplete.sass +17 -0
  40. data/lib/generators/kea/install/templates/_sherlock.sass +105 -0
  41. data/lib/generators/kea/install/templates/application.html.erb +38 -0
  42. data/lib/generators/kea/install/templates/global.js +7 -0
  43. data/lib/generators/kea/install/templates/init.js +18 -0
  44. data/lib/generators/kea/install/templates/main.js +18 -0
  45. data/lib/generators/kea/model/USAGE +7 -0
  46. data/lib/generators/kea/model/model_generator.rb +68 -0
  47. data/lib/generators/kea/model/templates/model.js.erb +96 -0
  48. data/lib/generators/kea/service/USAGE +7 -0
  49. data/lib/generators/kea/service/service_generator.rb +21 -0
  50. data/lib/generators/kea/service/templates/model.js.erb +58 -0
  51. data/lib/generators/kea/viewmodel/USAGE +6 -0
  52. data/lib/generators/kea/viewmodel/templates/viewmodel.js.erb +73 -0
  53. data/lib/generators/kea/viewmodel/viewmodel_generator.rb +21 -0
  54. data/lib/kea-rails/engine.rb +5 -0
  55. data/lib/kea-rails/version.rb +3 -0
  56. data/lib/kea-rails.rb +4 -0
  57. data/lib/tasks/kea_tasks.rake +4 -0
  58. metadata +115 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16389b697b4f38ceff8313c38e890be59cba06e1
4
+ data.tar.gz: 665a5552419bab02bc5cf9b8dd29351763568b14
5
+ SHA512:
6
+ metadata.gz: 806a1c1dbe01578fe421662d257d91c0ee1815181b6ee8ab623eb76aaecc4042363bede478788ad57ca9bd2f40a623ed5346f8228f4fae186bc5244d55e15b2c
7
+ data.tar.gz: e1af766999bd513b358d445911dec1e0ab03b89b2de62dbe6dce443c7b186efafe68bb31c33e6e801e3db08d087db1def2b89107f24ad492b9075848e11d3064
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Jan-Christian Föh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Kea
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Kea'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,24 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.activity = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $element = $(element),
7
+ activityState = ko.unwrap(valueAccessor());
8
+
9
+ $element.activityButton();
10
+ },
11
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
12
+ var $element = $(element),
13
+ activityState = ko.unwrap(valueAccessor());
14
+
15
+ if (activityState && !$element.activityButton().active()) {
16
+ $element.activityButton().on();
17
+
18
+ } else if (!activityState && $element.activityButton().active()) {
19
+ $element.activityButton().off();
20
+ }
21
+ }
22
+ };
23
+
24
+ })(ko, $);
@@ -0,0 +1,32 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.childVm = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var vmName = ko.unwrap(valueAccessor()),
7
+ parentVm = allBindingsAccessor().parentVm ? ko.unwrap(allBindingsAccessor().parentVm) : viewModel,
8
+ initVm = allBindingsAccessor().initVm ? ko.unwrap(allBindingsAccessor().initVm) : false,
9
+ setupContext = allBindingsAccessor().setupContext ? ko.unwrap(allBindingsAccessor().setupContext) : null,
10
+ childVm;
11
+
12
+ if (initVm) {
13
+ childVm = parentVm.addChildVm(vmName);
14
+ } else {
15
+ childVm = ko.utils.arrayFirst(parentVm._childVms(), function(item) {
16
+ return item._viewmodelName() === vmName;
17
+ });
18
+ }
19
+
20
+ if (setupContext && typeof childVm.setup === 'function') {
21
+ childVm.setup(setupContext);
22
+ }
23
+
24
+ // 'with' returns {controlsDescendants: true}, so we have to pass it up - otherwise,
25
+ // the descendant bindings will be bound twice ("You cannot apply bindings multiple times to the same element")
26
+ return ko.bindingHandlers.with.init(element, function() { return childVm; }, allBindingsAccessor, viewModel, bindingContext);
27
+ }
28
+ };
29
+
30
+ ko.virtualElements.allowedBindings.childVm = true;
31
+
32
+ })(ko, $);
@@ -0,0 +1,172 @@
1
+ (function(ko, $, app) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.komplete = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $container = $(element),
7
+ $searchInput = $container.find('> .komplete-search-term'),
8
+ listener = new window.keypress.Listener( $searchInput.get(0) ),
9
+ options = ko.unwrap(valueAccessor()),
10
+ AutocompleteVm,
11
+ AutocompleteResult,
12
+ vm,
13
+ vmName;
14
+
15
+ AutocompleteResult = function AutocompleteResult(label, value, rawResult) {
16
+ this.label = label;
17
+ this.value = value;
18
+ this.rawResult = rawResult;
19
+ this.hasFocus = ko.observable(false);
20
+ };
21
+
22
+ AutocompleteVm = function AutocompleteVm(url, autoSelect, selectCallback) {
23
+ this.url = url;
24
+ this.autoSelect = autoSelect;
25
+ this.selectCallback = selectCallback;
26
+ this.responseMap = {label: 'label', value: 'value'};
27
+ this.searchTerm = ko.observable('').extend({ rateLimit: { timeout: 500, method: "notifyWhenChangesStop" } });
28
+ this.requestInProgress = ko.observable(false);
29
+
30
+ this.showDropdown = ko.computed(function() {
31
+ return this.searchResults().length > 0;
32
+ }, this, {deferEvaluation: true});
33
+
34
+ this.searchTerm.subscribe(function() {
35
+ if (this.searchTerm().length > 0) {
36
+ this.fetch();
37
+ }
38
+ }, this);
39
+
40
+ this.searchResults = ko.observableArray([]);
41
+ };
42
+
43
+ AutocompleteVm.prototype.focusedResult = function focusedResult(results) {
44
+ return ko.utils.arrayFirst(this.searchResults(), function(result) { return result.hasFocus(); });
45
+ };
46
+
47
+ AutocompleteVm.prototype.focusOffset = function focusOffset(idxOffset) {
48
+ var currentFocus = this.focusedResult(),
49
+ focusedIdx = currentFocus ? this.searchResults().indexOf(currentFocus) : -1,
50
+ futureFocus = focusedIdx !== -1 ? this.searchResults()[focusedIdx + idxOffset] : null;
51
+
52
+ if (!futureFocus) { return; }
53
+
54
+ currentFocus.hasFocus(false);
55
+ futureFocus.hasFocus(true);
56
+ };
57
+
58
+ AutocompleteVm.prototype.focusNext = function focusNext() {
59
+ this.focusOffset(1);
60
+ };
61
+
62
+ AutocompleteVm.prototype.focusPrevious = function focusPrevious() {
63
+ this.focusOffset(-1);
64
+ };
65
+
66
+ AutocompleteVm.prototype.select = function select(result) {
67
+ this.selectCallback.call(bindingContext.$data, result);
68
+ this.reset();
69
+ };
70
+
71
+ AutocompleteVm.prototype.reset = function reset() {
72
+ this.searchTerm('');
73
+ this.searchResults.removeAll();
74
+ this.requestInProgress(false);
75
+ };
76
+
77
+ AutocompleteVm.prototype.fetch = function fetch(results) {
78
+ var that = this;
79
+
80
+ this.requestInProgress(true);
81
+
82
+ app.services.Base._request('GET', this.url, { q: this.searchTerm() })
83
+ .done(function(response) {
84
+ that.searchResults( that.parseResults(response) );
85
+
86
+ if (that.autoSelect && that.searchResults()[0]) {
87
+ that.searchResults()[0].hasFocus(true);
88
+ }
89
+ })
90
+ .always(function() {
91
+ that.requestInProgress(false);
92
+ });
93
+ };
94
+
95
+ AutocompleteVm.prototype.parseResults = function parseResults(results) {
96
+ var labelKey = this.responseMap.label,
97
+ valueKey = this.responseMap.value;
98
+
99
+ return results.map(function(result) {
100
+ return new AutocompleteResult(result[labelKey], result[valueKey], result);
101
+ });
102
+ };
103
+
104
+ if (DEBUG) { console.assert(options.url || options.fetch || options.vm, "Provide either an autocomplete URL, a custom fetch method or a custom AutocompleteVM"); }
105
+ if (DEBUG) { console.assert(options.onSelect, "onSelect callback missing"); }
106
+
107
+ options.autoSelect = typeof options.autoSelect !== 'undefined' ? options.autoSelect : true;
108
+ options.blurOnSelect = typeof options.blurOnSelect !== 'undefined' ? options.blurOnSelect : true;
109
+
110
+ vm = options.vm || new AutocompleteVm(options.url, options.autoSelect, options.onSelect);
111
+
112
+ if (options.fetch) {
113
+ vm.fetch = options.fetch;
114
+ }
115
+
116
+ if (options.responseMap) {
117
+ vm.responseMap = options.responseMap;
118
+ }
119
+
120
+ if (options.parseResults) {
121
+ vm.parseResults = options.parseResults;
122
+ }
123
+
124
+ listener.register_combo({
125
+ keys: "down",
126
+ on_keydown: function() { vm.focusNext(); }
127
+ });
128
+
129
+ listener.register_combo({
130
+ keys: "up",
131
+ on_keydown: function() { vm.focusPrevious(); }
132
+ });
133
+
134
+ listener.register_combo({
135
+ keys: "enter",
136
+ on_keydown: function() {
137
+ if (!options.autoSelect && !vm.focusedResult()) {
138
+ if (options.blurOnSelect) {
139
+ $searchInput.get(0).blur();
140
+ }
141
+
142
+ vm.select( $searchInput.val() );
143
+
144
+ } else if (options.autoSelect && !vm.focusedResult()) {
145
+ return;
146
+
147
+ } else {
148
+ if (options.blurOnSelect) {
149
+ $searchInput.get(0).blur();
150
+ }
151
+
152
+ vm.select( vm.focusedResult() );
153
+ }
154
+ }
155
+ });
156
+
157
+ $container.on('click', function() {
158
+ $searchInput.focus();
159
+ });
160
+
161
+ ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
162
+ listener.destroy();
163
+ });
164
+
165
+ return ko.bindingHandlers.with.init(element, function() { return vm; }, allBindingsAccessor, viewModel, bindingContext);
166
+ },
167
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
168
+
169
+ }
170
+ };
171
+
172
+ })(ko, $, window.app);
@@ -0,0 +1,47 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.sherlock = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $container = $(element),
7
+ options = ko.unwrap(valueAccessor()),
8
+ $providerSearchField,
9
+ sherlockVm;
10
+
11
+ sherlockVm = new app.sherlock.SherlockVm();
12
+ sherlockVm.setup();
13
+
14
+ sherlockVm.params.subscribe(function(params) {
15
+ bindingContext.$data.sherlockParams(params);
16
+ });
17
+
18
+ ko.renderTemplate('sherlock-container-template', sherlockVm, {}, element);
19
+
20
+ $container.addClass('sherlock-search-bar');
21
+
22
+ $providerSearchField = $container.find('.provider-search-field > input');
23
+
24
+ $providerSearchField.on('focus', function() {
25
+ sherlockVm.providerSearchActive(true);
26
+ });
27
+
28
+ $providerSearchField.on('blur', function() {
29
+ // if we do not delay this, it will fire before the 'click'
30
+ // event on dropdown list entries, clearing the list
31
+ setTimeout(function() {
32
+ sherlockVm.providerSearchActive(false);
33
+ }, 200);
34
+ });
35
+
36
+ $container.on('click', function() {
37
+ $providerSearchField.focus();
38
+ });
39
+
40
+ return { controlsDescendantBindings: true };
41
+ },
42
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
43
+
44
+ }
45
+ };
46
+
47
+ })(ko, $);
@@ -0,0 +1,57 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.sherlockProviderSearch = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $container = $(element).find('.provider-search-dropdown'),
7
+ $providerSearchField = $(element).find('> input[type=text]'),
8
+ sherlockVm = bindingContext.$data,
9
+ listener = new window.keypress.Listener($providerSearchField.get(0)),
10
+ keyCombos;
11
+
12
+ listener.register_combo({
13
+ keys: "down",
14
+ on_keydown: function() { sherlockVm.focusNextFragment(); }
15
+ });
16
+
17
+ listener.register_combo({
18
+ keys: "up",
19
+ on_keydown: function() { sherlockVm.focusPreviousFragment(); }
20
+ });
21
+
22
+ listener.register_combo({
23
+ keys: "enter",
24
+ on_keydown: function() { $providerSearchField.get(0).blur(); sherlockVm.selectFocusedFragment(); }
25
+ });
26
+
27
+ listener.register_combo({
28
+ keys: "backspace",
29
+ on_keydown: function() {
30
+ if (sherlockVm.providerSearchTerm().length === 0) {
31
+ sherlockVm.removeLastActiveFragment();
32
+ }
33
+
34
+ return true;
35
+ }
36
+ });
37
+
38
+ ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
39
+ listener.destroy();
40
+ });
41
+
42
+ },
43
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
44
+ var $container = $(element).find('.provider-search-dropdown'),
45
+ sherlockVm = bindingContext.$data,
46
+ providerSearchActive = sherlockVm.providerSearchActive(),
47
+ fragmentsForSearchTerm = sherlockVm.fragmentsForSearchTerm();
48
+
49
+ if (providerSearchActive && (fragmentsForSearchTerm.length > 0 || sherlockVm.liveSearchProviders().length > 0)) {
50
+ $container.addClass('active');
51
+ } else {
52
+ $container.removeClass('active');
53
+ }
54
+ }
55
+ };
56
+
57
+ })(ko, $);
@@ -0,0 +1,33 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.submitButton = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $element = $(element),
7
+ boundModel = ko.unwrap(valueAccessor()),
8
+ onSubmitSuccess = allBindingsAccessor().onSubmitSuccess || null;
9
+
10
+ $element.activityButton();
11
+
12
+ if (onSubmitSuccess) {
13
+ boundModel.submitComplete.subscribe(function(submitState) {
14
+ if (submitState) {
15
+ onSubmitSuccess($element, boundModel);
16
+ }
17
+ });
18
+ }
19
+
20
+ ko.bindingHandlers.click.init(element, function() { return boundModel.submit; }, allBindingsAccessor, viewModel, bindingContext);
21
+ },
22
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
23
+ var $element = $(element),
24
+ boundModel = ko.unwrap(valueAccessor()),
25
+ isActive = ko.unwrap(boundModel.submitInProgress),
26
+ isSubmittable = ko.unwrap(boundModel.isSubmittable);
27
+
28
+ isActive ? $element.activityButton().on() : $element.activityButton().off();
29
+ isSubmittable ? $element.removeClass('disabled') : $element.addClass('disabled');
30
+ }
31
+ };
32
+
33
+ })(ko, $);
@@ -0,0 +1,38 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.validationState = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var $element = $(element),
7
+ tooltipPosition = allBindingsAccessor().tooltipPosition || 'center top';
8
+
9
+ $element.attache({popoverClass: 'formerror', position: tooltipPosition});
10
+
11
+ ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
12
+ $element.attache().destroy();
13
+ });
14
+ },
15
+ update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
16
+ var $element = $(element),
17
+ $parentLabel = $element.parent('label'),
18
+ hasParentLabel = $parentLabel.length > 0,
19
+ $cssClassLocation = hasParentLabel ? $parentLabel.find('span.custom') : $element,
20
+ $errorMessage = hasParentLabel ? $parentLabel.find('span.error.message') : $element.next('span.error.message'),
21
+ observable = valueAccessor(),
22
+ hasError = ko.unwrap(observable.hasError ? observable.hasError : observable.hasErrors),
23
+ messageText = ko.unwrap(observable.validationMessage ? observable.validationMessage : observable.validationMessages);
24
+
25
+ if (typeof hasError === 'undefined' || !hasError) {
26
+ $cssClassLocation.removeClass('error');
27
+ $element.attache().hide();
28
+
29
+ } else {
30
+ $cssClassLocation.addClass('error');
31
+
32
+ $element.attache().setContent(messageText);
33
+ $element.attache().show();
34
+ }
35
+ }
36
+ };
37
+
38
+ })(ko, $);
@@ -0,0 +1,44 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.bindingHandlers.waitForVm = {
5
+ init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
6
+ var vmName = ko.unwrap(valueAccessor()),
7
+ subscription,
8
+ childVm;
9
+
10
+ childVm = ko.utils.arrayFirst(viewModel._childVms(), function(item) {
11
+ return item._viewmodelName() === vmName;
12
+ });
13
+
14
+ // if the child viewmodel isn't available yet...
15
+ if (!childVm) {
16
+
17
+ // subscribe to the list of child viewmodels
18
+ subscription = viewModel._childVms.subscribe(function() {
19
+ childVm = ko.utils.arrayFirst(viewModel._childVms(), function(item) {
20
+ return item._viewmodelName() === vmName;
21
+ });
22
+
23
+ // and if it has become available now, remove the subscription,
24
+ // apply the bindings to our child elements and make ourselves visible
25
+ if (childVm) {
26
+ subscription.dispose();
27
+ ko.applyBindingsToDescendants(bindingContext, element);
28
+ ko.bindingHandlers.visible.update(element, function() { return childVm; }, allBindingsAccessor, viewModel, bindingContext);
29
+ }
30
+ });
31
+ }
32
+
33
+ // hide ourselves and our children if the viewmodel isn't
34
+ // immediately available on init().
35
+ ko.bindingHandlers.visible.update(element, function() { return childVm; }, allBindingsAccessor, viewModel, bindingContext);
36
+
37
+ // prevent our descendant elements from being bound if the
38
+ // viewmodel isn't immediately available on init(). We will
39
+ // take care of that later ourselves through ko.applyBindingsToDescendants
40
+ return {controlsDescendantBindings: !childVm};
41
+ }
42
+ };
43
+
44
+ })(ko, $);
@@ -0,0 +1,85 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.extenders.equals = function(target, options) {
5
+ ko.utils.validatorBase(target);
6
+
7
+ var defaults = {
8
+ message: "",
9
+ to: null
10
+ };
11
+
12
+ options = $.extend({}, defaults, options);
13
+
14
+ if (!options.to) {
15
+ if (DEBUG) { console.error("equality validator is missing its comparison target"); }
16
+ }
17
+
18
+ target.shouldValidate = function shouldValidate(comparisonValueHasCHanged) {
19
+
20
+ // don't validate if the comparison value
21
+ // a) is invalid or unvalidated
22
+ // b) was changed, but this value has not been validated before
23
+ if (ko.isObservable(options.to)) {
24
+ if ( ( options.to.isUnvalidated() || options.to.hasError() ) ||
25
+ ( comparisonValueHasCHanged && target.isUnvalidated() ) ) {
26
+
27
+ return false;
28
+ }
29
+ }
30
+
31
+ return true;
32
+ };
33
+
34
+ target.performValidation = function performValidation(validatableValue, comparisonValue) {
35
+ if (options.allowBlank && (!validatableValue || validatableValue === '')) {
36
+ target.markValid();
37
+
38
+ return !target.hasError();
39
+ }
40
+
41
+ if (validatableValue === comparisonValue) {
42
+ target.markValid();
43
+ } else {
44
+ target.markInvalid(options.message);
45
+ }
46
+
47
+ return !target.hasError();
48
+ };
49
+
50
+ target.validate = function validate(newValue, newComparisonValue, comparisonValueHasCHanged) {
51
+ var validatableValue = typeof newValue === 'undefined' ? target() : newValue,
52
+ comparisonValue = typeof newComparisonValue === 'undefined' ? ko.unwrap(options.to) : newComparisonValue;
53
+
54
+ if (!target.shouldValidate(comparisonValueHasCHanged)) {
55
+ return;
56
+ }
57
+
58
+ return target.performValidation(validatableValue, comparisonValue);
59
+ };
60
+
61
+ target.forceValidate = function forceValidate(newValue, newComparisonValue, comparisonValueHasCHanged) {
62
+ var validatableValue = typeof newValue === 'undefined' ? target() : newValue,
63
+ comparisonValue = typeof newComparisonValue === 'undefined' ? ko.unwrap(options.to) : newComparisonValue;
64
+
65
+ return target.performValidation(validatableValue, comparisonValue);
66
+ };
67
+
68
+ target.subscribe(target.validate);
69
+
70
+ // trigger re-validation if the comparison value changes, too.
71
+ // Only do this if this value has been validated before, or we risk throwing
72
+ // an error before the user had time to fill out this field
73
+ if (ko.isObservable(options.to)) {
74
+ options.to.subscribe(function() {
75
+ if (typeof target !== 'undefined') {
76
+ target.validate(undefined, undefined, true);
77
+ }
78
+ });
79
+ }
80
+
81
+ //return the original observable
82
+ return target;
83
+ };
84
+
85
+ })(ko, $);
@@ -0,0 +1,65 @@
1
+ (function(ko, $) {
2
+ "use strict";
3
+
4
+ ko.extenders.externalValidator = function(target, options) {
5
+ ko.utils.validatorBase(target);
6
+
7
+ var defaults = {
8
+ message: "",
9
+ validator: function(v) {},
10
+ dependencies: []
11
+ };
12
+
13
+ options = $.extend({}, defaults, options);
14
+
15
+ target.validate = function validate(newValue) {
16
+ var validatableValue = typeof newValue === 'undefined' ? target() : newValue,
17
+ validatorResponse = options.validator(validatableValue),
18
+ validatorResponseHandler;
19
+
20
+ validatorResponseHandler = function validatorResponseHandler(response) {
21
+ var result, message;
22
+
23
+ if (typeof response === 'boolean') {
24
+ result = response;
25
+ message = options.message;
26
+
27
+ } else {
28
+ result = response.result;
29
+ message = response.message;
30
+ }
31
+
32
+ if (result) {
33
+ target.markValid();
34
+ } else {
35
+ target.markInvalid(message);
36
+ }
37
+ };
38
+
39
+ if (typeof validatorResponse === 'object' && typeof validatorResponse.done === 'function') {
40
+ target.validationInProgress(true);
41
+ validatorResponse.done(function(response) { validatorResponseHandler(response); target.validationInProgress(false); });
42
+ return;
43
+
44
+ } else {
45
+ validatorResponseHandler(validatorResponse);
46
+ return !target.hasError();
47
+ }
48
+ };
49
+
50
+ //validate whenever the value changes
51
+ target.subscribe(target.validate);
52
+
53
+ ko.utils.arrayForEach(options.dependencies, function(dependency) {
54
+ dependency.subscribe(function(newValue) {
55
+ if (!target.isUnvalidated()) {
56
+ target.validate();
57
+ }
58
+ });
59
+ });
60
+
61
+ //return the original observable
62
+ return target;
63
+ };
64
+
65
+ })(ko, $);