opitzconsulting / jquery-mobile-angular-adapter

jquery mobile angular adapter
MIT License
517 stars 114 forks source link

How to prevent repeat init controller when visit the page via $.mobile.changePage()? #134

Closed giver closed 11 years ago

giver commented 11 years ago

Everytime I visit the the login page Ex. $.mobile.changePage('login.html'). I see that the method init was called again (I think controller was re-initialised again).

Question.

Note. I have one controller per page per file.

login.html

<body>
<div data-role="page" id="login" ng-controller="LoginCtrl">            
<!-- some html -->
</div>
</body>

login.js

function LoginCtrl($scope, $http) {
// Somecode
$scope.init = function() { alert('init'); }
$scope.init();
}
tbosch commented 11 years ago

Hi, this is due to the dom caching of jquery mobile, which is deactivated by default. By this, visited external pages are removed from the cache as soon as they are left (this does not apply to pages declared in the index.html). To change the cache settings of jquery mobile, see here: http://jquerymobile.com/demos/1.2.0/docs/pages/page-cache.html

Hope this helps, Tobias

giver commented 11 years ago

Hi Tobias, Thanks for your suggestion.

I think my question is not clear enough. Let me explain more.

1) From README.md, I wonder where is 'pageinit' implement in adapter? I see you describe only ngm-pagebeforeshow, ngm-pagebeforehide,ngm-pageshow, ngm-pagehide but not something similar to ngm-pageinit. So back to my question. I want to know if I could have something similar to 'pageinit'

2) I found that my controller was interpret every time I visit the page via ajax loadpage. So, I can assume that behavior make me think like a 'pageinit'. Please correct me.

3) How can I make my controller Singleton? (I'm not sure if this question belong to AngularJS not this adapter)

tbosch commented 11 years ago

Hi, About 1) und 2): if you want to react to pageinit, yes, this is just the time when the controller gets called. pageinit was intentionally skipped as you can do everything just in the controller function.

3) If your controller is at a page, it can't be a singleton, as it is tied to the page, which is recreated depending on the jQuery page cache: only once for embedded pages, for external pages depending on the cache settings. You could use a service (which is a singleton), and then use the controller just to delegate to that service. Another way is using a shared controller, which are introduced by the adapter (see here:https://github.com/tigbro/jquery-mobile-angular-adapter#directive-ngm-shared-controller).

Sorry for the late answer, Tobias

tbosch commented 11 years ago

Closing this, hope my answer was enough...

Tobias