Ending an Ember application - ember.js

What's the proper way to end an Ember application?
In my application, I have a logout button. When pressed, the app sends a message to the server, which terminates the user session and does some cleanup. On the client side, I currently just transition to the login route, which doesn't really terminate the application since artifacts from the prior login remains. Hitting the refresh button manually would do it. Does Ember provide a function to programmatically refresh a page? Is there a more elegant way of doing this than a refresh?

There's a couple of things you'd need to do.
To reset Ember itself you can just use App.reset();
Reset any client side record management software (this is different for Ember Model/Ember Data).
Ember doesn't provide a way to programmatically refresh a page, since you can just use window.location.reload().

Related

Electron disable cookies

I was wondering if it's possible to enable/disable cookies within the chromium plugin browser for electron.
My application uses a third party authentication and renders a pop up window for it. I want to disable the cookies when the user is authenticating and then re-enable it after its completes since i only care about the tokens. There are other reasons for this that I don't want to go into with the way the app is designed.
The application use https://github.com/electron/electron/blob/master/docs/api/session.md. It talks about how to add/get/delete cookies, but not how to disable/enable it.
Any advice appreciated,
Thanks,
D
Most of the third party auth providers require cookies to work correctly. For example, some auth providers like live.com generate initial cookies before the user gets to the login page. These cookies are required when the login button is clicked.
The question itself is mute at this point, but it's interesting why electron doesn't give the user the ability to disable/enable cookies thou since it's just a shell for chromium which has the feature. Perhaps the use case isn't high.

Ember Simple Auth when all pages requires authorization

I have website that requires authorization on all its pages. To achieve that there is server that response based on cookie. If there is cookie send ember app, login site otherwise. This allows to facebook like behavior. I am using ember-simple-auth addon to help with authorization.
Since user is never inside app without being successfully authorized, one should be able to save ember-data record object into session. This works nice for one tab but breaks horrible with multiple tabs. Is there way how to have ember-data objects in session and yet support multiple browser tabs?
Edit:
I maybe found workaround, save index into session and use peekRecord in computed property.

How to detect if the user have come from external link?

Is there a way to distinguish access through external link (e.g. from history, or from other site) or access by link from other page of the same app in beforeModel handler?
Here is my case:
If user accesses my app by direct link and requests route requiring authenticated user, I make a transition to index route and show popup with an authorization form.
If user is already on my site and trying to access the requested route requiring authenticated user, I abort transition and show the popup on a previous page.
I have no separate page for authentication.
My current work around is to check some strange private property called sequence of transition object which has been passed to beforeModel handler, if transition.sequence === 0, then user came from "outside", otherwise user follow by link "inside" my app.
I think this is a bad way to achieve the goal, core developers can remove sequence property any time.
So, is there any clean way to distinguish whether current route was requested from "outside" or "inside" of my app?
I'll appreciate any help, thanks!
P.S. EmberJS v1.10.0
P.S.S. Sorry for my English
If you want to avoid using an internal call you could keep track of the history in the application route something like this Implementing a "conditional" back button in ember.js
But in your router would probably be better since controllers are going away soonish.
You could probably use http://emberjs.com/api/classes/Ember.Route.html#event_didTransition
UPDATE after chatting
Logic for counting, aborting, etc transitions can be done in the willTransition action in application route http://emberjs.com/guides/routing/preventing-and-retrying-transitions/

Ember / browser refresh button / reload entire site / resetting everything. How to override that behavior?

Apparently when you hit the refresh button, the browser reloads the entire site (with all the scripts), resetting everything, including the "loggedInUser" I put in the application controller.... Is it the browser thing? Or ember thing?
How to stop any of them / both to just stop doing that...? Or should I take the non-trivial route of storing stuffs in browser local storage, etc? Really?
Thanks,
Raka
It's not too difficult to keep the state of your app on browser refresh. I chose to store an auth token, user id, and user role in cookies. Here is an Auth Manager object that can be used to manage the client side of user authentication.
Gist auth_manager.js
I instantiate it when the app loads, and call it's 'authenticate' method on init, and when a user successfully authenticates from the server, like in my login or sign up controllers.
I hope this points you in the right direction.

Ember store (using localstorage adapter) data is lost after browser refresh

I configured my Ember App to run with the localstorage_adapter.
I am new to Ember and probably, I don't grasp all its details yet. Still, from what I understand, when you refresh the browser the ember store data should not be lost. Obviously the localstorage is not affected by a browser refresh.
This is what happens:
1. after login, I fetch the data from the server and store it using store.push() or store.pushMany()
2. play around with the application
3. at some point hit a browser refresh (or on my Android phone when I hit the home button, the browser get's closed and when I open it again from the running applications window, it opens and does a refresh by default)
4. the page which I was on gets messed up
For example, I'm on page where I display the user name, a question and a list of possible answers. Please note that there are 20+ questions in store. I know this by using Ember Inspector. I hit refresh, now the store has 1 user entity (the logged user) which is fine, 1 question only (not good at all), the one which was displayed on the page, and the answers store is empty. All other entities (which have nothing to do with this page) are gone too! So what I can see on my web page is the user name and the question's text, no answers, no way to move forward.
Is this supposed to happen? The "in memory" store gets cleared and not repopulated from localstorage?
thanks!
Is this supposed to happen? The "in memory" store gets cleared and not repopulated from localstorage?
Yes, it is supposed to happen. To understand why, first remember that the localStorage in your browser is seen as a server. To Ember-Data, there's absolutely no difference between your localStorage and a Rails server with a REST API. Ember-Data treats them both as asynchronous data stores that it communicates with through an adapter. The LocalStorageAdapter is no different.
What does this mean? It means that your store only gets populated with records that you specifically request. Just as Ember-Data doesn't try to get all of the records from a Rails server at startup, it doesn't try to get all of the records from your localStorage at startup. This is by design. Ember-Data doesn't play favorites; it treats all adapters equally.
In order to repopulate your store with entries from the localStorage, use the store's find() method like you would in any other situation. The calls with be quick, although still asynchronous.
Also, as Beerlington mentioned, you must save your data explicitly if you want it to go into the localStorage. The store won't place it there automatically.