MVC violation in Ionic(angular) framework with $ionicModal

梦想与她 提交于 2019-12-06 11:42:49
piwi

I stumbled on your question while trying to come up with a solution similar to your concern.

Because I had a problem regarding navigation in my routes, I decided to use $ionicModal to show a view of another state in a modal view. I came up with a solution I crafted there (but I did not implement it for my working context yet) that should work in my case, while I'm not really satisfied with it.

To summarize, all my states are nested under tabs; when I am in the tabs.home state, I want to directly show the tabs.settings.sub state. However, tabs.settings.sub relies on data populated by its parent state tabs.settings. Hence my problem with giving the scope of my current state (tabs.home) to tabs.settings.sub.

My modal uses a template that will include the template of my view:

<script id="templates/modal.html" type="text/ng-template">
  <ion-modal-view>
    <ng-include src="templateUrl" ng-controller="controller"></ng-include>
  </ion-modal-view>
</script>

I can then reuse the view from the state. Regarding the scope, I used $scope.new(true) to isolate it, and populated it with data required by my modal template:

var subState = $state.get ('tabs.settings.sub');
var subScope = $scope.$new (true); // true: isolate
subScope.title = 'Sub';
subScope.templateUrl = subState.templateUrl;
subScope.controller = function () {
  if (subState.controller)
    return $controller (subState.controller, {$scope:subScope});
  return null;
};

The modal is instantiated using this scope (that's one problem to my opinion: mixing the scope of the modal and the scope of the controller). The controller has to be a function that returns the appropriate controller.

$ionicModal.fromTemplateUrl ('templates/modal.html', {
  scope: subScope
}).then (function (modal) {
  modal.show ();
});

The major problem with my solution is to transit data up to the controller of the view to show (in this case SubCtrl). But it is more narrowed to my specific context: my modal is not aware of the chain of inheritance of controllers adn states, because this is handled by UI router.

I don't know if it is possible to access the state associated to a controller (the usual pattern seems to be to use $state.parent, but this cannot be used here, as mentioned by the UI router wiki).

The workaround I use here (this is the part I am not satisfied with) is to federate data through the states:

.state ('tabs.settings', {
  data: { status: 'valid' }
}

I have access to it when creating my modal:

subScope.status = subState.data.status;

And I have access to it from the parent controller:

$scope.status = $state.current.data.status;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!