Load view in background based on data from initial login view

286 views Asked by At

This is not my actual use case, but I am trying to understand the concept of how to load a view based on the information another view has using appgyver supersonic framework.

I have 3 pages, Login.html, MainPage.html, and Load.html. Each of them have their own controller. When the app opens it first goes to the login page. The login page sends the username and password to my server which then returns success or not. When there is success, the app goes to the main page. I would like for the load.html to understand a login has happened and start loading the view with the users appropriate content. This is so when the user goes from the main page to the load page all the information is already loaded. I am trying to understand the concept of how to accomplish this with supersonic.

1

There are 1 answers

0
area28 On BEST ANSWER

You can use publish / subscribe methods between controllers.

Check out this doc for some good examples.

Basically, you want to publish a message with some data when the user is logged in. Your controller for after log in will be able to listen or subscribe to that message. When it receives the message it will be able to run any code you tell it in the callback.

One controller will publish:

// gather some user data with successful login like var id = result.user.id
supersonic.data.channel('loginSuccess').publish({ userid: id });

Another controller will listen:

supersonic.data.channel('loginSuccess').subscribe( function(data) {
    $scope.id = data.userid;
    // load some data based on user id
});

You can preload the views as well. This may help speed things up. If you don't, you may see a spinner in between view pushes. In structure.coffee in the config folder:

preloads: [
  {
    id: "viewIdYouSet"
    location: "nameOfModule#view"
  }
]

When the login finishes, you can find the preloaded view and push it to the stack:

supersonic.ui.views.find("viewIdYouSet").then( function(startedView) {
    supersonic.ui.layers.push(startedView);
});

Hope this helps.