@@ -652,6 +652,49 @@ ko.dirtyFlag = function(root, isInitiallyDirty) {
652
652
return result ;
653
653
} ;
654
654
655
+ /**
656
+ * A simple dirty flag that will detect the first change to a model, then afterwards always return true (meaning
657
+ * dirty). This is to prevent the full model being re-serialized to JSON on every change, which can cause
658
+ * performance issues for large models.
659
+ * From: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html
660
+ * @param root the model.
661
+ * @returns true if the model has changed since this function was added.
662
+ */
663
+ ko . simpleDirtyFlag = function ( root ) {
664
+ var _initialized = ko . observable ( false ) ;
665
+
666
+ // this allows for models that do not have a modelAsJSON method
667
+ var getRepresentation = function ( ) {
668
+ return ( typeof root . modelAsJSON === 'function' ) ? root . modelAsJSON ( ) : ko . toJSON ( root ) ;
669
+ } ;
670
+
671
+ var result = function ( ) { } ;
672
+
673
+ //one-time dirty flag that gives up its dependencies on first change
674
+ result . isDirty = ko . computed ( function ( ) {
675
+ if ( ! _initialized ( ) ) {
676
+
677
+ //just for subscriptions
678
+ getRepresentation ( ) ;
679
+
680
+ //next time return true and avoid ko.toJS
681
+ _initialized ( true ) ;
682
+
683
+ //on initialization this flag is not dirty
684
+ return false ;
685
+ }
686
+
687
+ //on subsequent changes, flag is now dirty
688
+ return true ;
689
+ } ) ;
690
+ result . reset = function ( ) {
691
+ _initialized ( false ) ;
692
+ }
693
+
694
+ return result ;
695
+ } ;
696
+
697
+
655
698
656
699
/**
657
700
* A vetoableObservable is an observable that provides a mechanism to prevent changes to its value under certain
@@ -963,3 +1006,27 @@ function activityProgressClass(progress) {
963
1006
return ACTIVITY_PROGRESS_CLASSES [ progress ] ;
964
1007
}
965
1008
1009
+ ko . bindingHandlers . numeric = {
1010
+ init : function ( element , valueAccessor ) {
1011
+ $ ( element ) . on ( "keydown" , function ( event ) {
1012
+ // Allow: backspace, delete, tab, escape, and enter
1013
+ if ( event . keyCode == 46 || event . keyCode == 8 || event . keyCode == 9 || event . keyCode == 27 || event . keyCode == 13 ||
1014
+ // Allow: Ctrl+A
1015
+ ( event . keyCode == 65 && event . ctrlKey === true ) ||
1016
+ // Allow: . ,
1017
+ ( event . keyCode == 190 || event . keyCode == 110 ) ||
1018
+ // Allow: home, end, left, right
1019
+ ( event . keyCode >= 35 && event . keyCode <= 39 ) ) {
1020
+ // let it happen, don't do anything
1021
+ return ;
1022
+ }
1023
+ else {
1024
+ // Ensure that it is a number and stop the keypress
1025
+ if ( event . shiftKey || ( event . keyCode < 48 || event . keyCode > 57 ) && ( event . keyCode < 96 || event . keyCode > 105 ) ) {
1026
+ event . preventDefault ( ) ;
1027
+ }
1028
+ }
1029
+ } ) ;
1030
+ }
1031
+ } ;
1032
+
0 commit comments