I discovered a (in my opinion) weird behavior. If I open a dialogService from my plugin I pass a specific part of my $scope.model.value (an array with nested objects in this case) like this:
angular.module("umbraco").controller("myController", function ($scope, dialogService) {
// Main controller
$scope.doSomething = function (index) {
dialogService.open({
template: '/path/to/template.html',
show: true,
modelData: $scope.model.value.array[index],
callback: function (data) {
$scope.model.value.array[index] = data;
}
});
};
}
angular.module("umbraco").controller("myDialogController", function ($scope) {
// Dialog controller
if ($scope.$parent.dialogOptions.modelData === null || $scope.$parent.dialogOptions.modelData === undefined) {
$scope.modelData = [];
} else {
$scope.modelData = $scope.$parent.dialogOptions.modelData;
}
// some method
$scope.myFunction = function () {
// create new object
var item = { prop1: "", prop2: "" };
// push to array
$scope.modelData.push(item)
}
}
The behavior I would expect is the following:
I edit some/none/all of the data in my dialog, click submit and
then the changed data are given back to my model in the plugin (in this case a new object was added to the array).
What actually happens:
I edit some/none/all of the data in my dialog and the plugin model is
updated instantly (the object is pushed to my plugin view model as soon as I push it to array in the plugin).
This means for example, if I accidentally delete something in my dialog it can't be undone by just pressing "cancel".
Should I use model.control instead and just "sync" it onSubmit? But if so, how could that be achieved? Or am I missing out some important architectural feature from umbraco/angularjs at this point?
i think the issue is that you are accessing the parent model, which is linked directly to you $scope.model.
You can pass data to your dialog by settings the dialogData variable (not really documented as far as i can tell, but this appears to be how the core does it.)
you should then be able to access a detached version of this via $scope.dialogData
you can then manipulate this value without changing the one on your parent page - returning the data into the callback and setting it on the item there.
You should now be able to access $scope.dialogData as your model, binding back to the parent with $scope.$parent will update the main view so should be avoided.
And after doing all the fantastic angular stuff with it, I simply passed it back with save(modelData). This ensures, that the model in my main view is only updated after the user closes the dialog.
dialogService updates main view model instantly
I discovered a (in my opinion) weird behavior. If I open a
dialogService
from my plugin I pass a specific part of my$scope.model.value
(an array with nested objects in this case) like this:The behavior I would expect is the following:
I edit some/none/all of the data in my dialog, click submit and then the changed data are given back to my model in the plugin (in this case a new object was added to the array).
What actually happens:
I edit some/none/all of the data in my dialog and the plugin model is updated instantly (the object is pushed to my plugin view model as soon as I push it to array in the plugin).
This means for example, if I accidentally delete something in my dialog it can't be undone by just pressing "cancel".
Should I use
model.control
instead and just "sync" itonSubmit
? But if so, how could that be achieved? Or am I missing out some important architectural feature from umbraco/angularjs at this point?Hi
i think the issue is that you are accessing the parent model, which is linked directly to you $scope.model.
You can pass data to your dialog by settings the dialogData variable (not really documented as far as i can tell, but this appears to be how the core does it.)
you should then be able to access a detached version of this via $scope.dialogData
you can then manipulate this value without changing the one on your parent page - returning the data into the callback and setting it on the item there.
Hi Kevin,
many thanks for your fast reply. I gave it a try and implemented it like you were proposing:
But unfortunately the binding is still existing and updating my main view from inside the dialog if I change something.
did you also change this code ?
You should now be able to access
$scope.dialogData
as your model, binding back to the parent with$scope.$parent
will update the main view so should be avoided.Yes, I also changed the dialog controller. Please take a look at the last line of code on my previous reply. I just simplified the if/else construct.
So after I found no real solution for this problem, I managed to "fix" it with copying the datamodel to the scope in the dialog:
And after doing all the fantastic angular stuff with it, I simply passed it back with
save(modelData)
. This ensures, that the model in my main view is only updated after the user closes the dialog.is working on a reply...