ui-codemirror icon indicating copy to clipboard operation
ui-codemirror copied to clipboard

ngModel.$render()

Open loops-and-spells opened this issue 11 years ago • 4 comments

need ngModel.$render() after ngModel.$setViewValue newValue or model may not update values.

loops-and-spells avatar Jun 20 '14 20:06 loops-and-spells

This doesn't quite sound right. Can you provide an example of the model not updating correctly?

whitehat101 avatar Jun 23 '14 03:06 whitehat101

I will send from the office, I had to store the cursor position and call render or the data would not match up. It was a restangularized model so maybe something else is going on. On Jun 22, 2014 10:03 PM, "Jeremy" [email protected] wrote:

This doesn't quite sound right. Can you provide an example of the model not updating correctly?

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-codemirror/issues/58#issuecomment-46802649 .

loops-and-spells avatar Jun 23 '14 04:06 loops-and-spells

here are the changes i needed for the ngModel.$render override, calling $render would reset the cursor position.

        ngModel.$render = ->
          safeViewValue = ngModel.$viewValue or ""
          cursor_pos = codeMirror.getCursor()
          codeMirror.setValue ngModel.$viewValue
          codeMirror.setCursor(cursor_pos)

and here where the ngModel is synced from CodeMirror

            scope.$apply ->
              ngModel.$setViewValue newValue
              ngModel.$render() 

the issue I was having was when I submitted the model to a restful route, it was returning as an empty string.

Page is a restangular resource, and the model passed to the CodeMirror directive.

            $scope.page = Page
            $scope.onSavePage = () ->
              Page.put()

loops-and-spells avatar Jun 23 '14 16:06 loops-and-spells

Thanks! I'll look into this in detail this weekend. For the record, what versions of angular and codemirror are you using?

whitehat101 avatar Jun 25 '14 03:06 whitehat101