Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Model's value is not idempotent #156

Closed
slavafomin opened this issue Jul 30, 2015 · 3 comments
Closed

Model's value is not idempotent #156

slavafomin opened this issue Jul 30, 2015 · 3 comments

Comments

@slavafomin
Copy link

Hello!

I'm developer of Angular.js Input Modified directive. Our module is used to detect changes in the form fields. We heavily depend on the ngModel interface in our implementation and I wanted to say that you've made a great job of implementing it properly.

However, there's an issue with model value idempotency. Sometimes, when you take current value from the directive and then feed it back to it, you will get yet another value. It looks like TinyMCE is refactoring the HTML code when you feed it to it (reordering attributes for example). Maybe it's not a strict violation of the ngModel interface, but it breaks our model change detection mechanism (because returned value is not always equal to provided one).

For now, I have had to add a stabilization function to our module in order to overcome this problem. I suggest you to implement it in your code as well (for better compatibility).

/**
 * Stabilizes model's value.
 * This is required for directives such as Angular UI TinyMCE.
 */
function stabilizeValue (callback) {
  var initialValue = modelCtrl.$modelValue;
  modelCtrl.$modelValue = null;
  $timeout(function () {
    modelCtrl.$modelValue = initialValue;
    $timeout(callback, 0);
  }, 0);
}
@timscaffidi
Copy link

Possibly a related issue: I have a ui-tinymce in a form and it is bound like so:

ng-model="some.object.property"

Initially, the object $scope.some does exist, but $scope.some.object does not exist and is only created once a model value, like $scope.some.object.property is set.

It appears as though as soon as the ui-tinymce field is loaded, it will automatically set its model value to <p><br data-mce-bogus="1"></p> without any user interaction.

For my use case, I am relying on $scope.some.object not existing until the user does something to create it, and thus this behavior breaks my form's functionality.

@ryanberckmans
Copy link

@timscaffidi I believe your issue with ng-model="some.object.property" is unrelated. I had your issue, Tim, and fixed it by applying priority: 999, to the ui-tinymce directive. There is an open PR for this same fix.

I inserted a console log into ngModel.$formatters and noticed that, when my network call completed, some.object.property was executing the formatter, but then ngModel.$render was never called. Quick bit of googling suggested it was a directive priority issue. So I added priority: 999,.

@deeg
Copy link
Contributor

deeg commented Oct 16, 2015

@slavafomin, if you would like to get this into our library please update the issue with a Plunker or FIddle re-creating the issue.

It would also be nice if you want to open a pull request with how you think it should be implemented, and then I can take a look at getting it merged.

I'm not opposed to the idea if it really is an issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants