Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strange behaviour in Actions #20

Closed
vesparny opened this issue Feb 12, 2015 · 3 comments
Closed

strange behaviour in Actions #20

vesparny opened this issue Feb 12, 2015 · 3 comments

Comments

@vesparny
Copy link

I have this Action

'use strict';

var { Actions } = require('flummox');
var arrayUtils = require('../utils/arrayUtils');

class FavoritesActions extends Actions {

  addFavorite(id){
    var favorites = this.getAll();
    favorites.push(id);
    window.localStorage.setItem('favorites', JSON.stringify(favorites));
    return favorites;
  }

  removeFavorite(id){
    var favorites = this.getAll();
    arrayUtils.remove(favorites, id);
    window.localStorage.setItem('favorites', JSON.stringify(favorites));
    return favorites;
  }

  getAll(){
    return JSON.parse(window.localStorage.getItem('favorites')) || [];
  }
}

module.exports = FavoritesActions;

When I call the method addFavorite() internally I call the method this.getAll() and assign its result to favorites.
The problem is that favorites.push(id) seems to be executed before the method this.getAll() returns, like if it was async, and I get an error of course because favorites is undefined.

I don't know if the problem here is about something related to es6 which I'm a total noob at.

@acdlite
Copy link
Owner

acdlite commented Feb 12, 2015

@vesparny Which version of Flummox are you running? I believe this may have something to do with a bug that was fixed in version 2.3.

@acdlite
Copy link
Owner

acdlite commented Feb 12, 2015

@vesparny Oh, actually I see the problem now. All actions return undefined (or a promise that resolves to undefined) to their caller. This is by design, to enforce unidirectional data flow. Data fetching operations like getAll() should be in a separate class or module. Usually you'd call this something like WebAPIUtils. There's a section about this in the API docs for Actions: https://github.com/acdlite/flummox/blob/master/docs/api/Actions.md

@vesparny
Copy link
Author

@acdlite yep it was just that. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants