vendredi 21 avril 2017

ember: using cookies with ember-network

Can cookies be used with ember-network requests? Thanks to this answer I know that they can be used with ember-data API requests, but I need to do a network request in an initializer and it doesn't appear the ember-data store can be accessed that early.

Background:

I'm wanting to persist shopping cart data to the backend for these reasons

The ember-cart addon has a smart way of persisting the cart by jsonifying and data model and dumping to localstore when it changes:

window.localStorage.setItem('cart', JSON.stringify(this.payload()));

then upon return visit parsing the json and pushing it into the store in an instance initializer:

...
payload = JSON.parse(payload);
...
cart.pushPayload(payload);

I'd like to do basically the same thing, but instead of getting the JSON from localstorage, get it from the API via the network.

the store ins't available in an initializer, but ember-network is. So hypothetically I think I can do this. The problem I'm running into is that the cookie isn't being passed.

I get around this with ember-data by using this:

  xhrFields: {
    withCredentials: true
  }

in the application adapter, but I can't find any info about whether there's a similar setting for ember-network. I see the request to my API being made in the initializer, but the api doesn't return anything because the browser cookie isn't included.




Aucun commentaire:

Enregistrer un commentaire