
// see http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController
angular.module('palaso.ui.jqte', [])
	// jqte
	.directive('puiJqte', ['$parse', function($parse) {
		return {
			restrict: 'A',
			require: '?ngModel', // This causes the controller in the link function to be the ngModelController
			link: function(scope, element, attrs, ngModelCtrl) {
				if (!ngModelCtrl) {
					return;
				}
				ngModelCtrl.$render = function() {
					element.val(ngModelCtrl.$viewValue);
				};
				var options = scope[attrs.puiJqte] === undefined ? {} : scope[attrs.puiJqte];
				options.change = function() {
					var v = element.val();
					console.log('change', v);
					scope.$apply(read);
				}; 
				element.jqte(options);
				function read() {
					ngModelCtrl.$setViewValue(element.val());
				}
			}
		};
	}])
	;


// see http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController
angular.module('palaso.ui.jqte', [])
	// jqte
	.directive('puiJqte', ['$parse', function($parse) {
		return {
			restrict: 'A',
			require: '?ngModel', // This causes the controller in the link function to be the ngModelController
			link: function(scope, element, attrs, ngModelCtrl) {
				if (!ngModelCtrl) {
					return;
				}
				ngModelCtrl.$render = function() {
					var phase = scope.$$phase;
					console.log('render ', phase);
					element.jqteVal(ngModelCtrl.$viewValue);
				};
				var options = scope[attrs.puiJqte] === undefined ? {} : scope[attrs.puiJqte];
				options.change = function() {
					if (scope.$$phase == '$digest') {
						return;
					}
					scope.$apply(read);
				}; 
				element.jqte(options);
				read();
				function read() {
					ngModelCtrl.$setViewValue(element.val());
				}
			}
		};
	}])
	;

Fix ng-jqte directive that was not binding in both directions.