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

How can I open a prompt in a certain state? #61

Closed
uusijani opened this issue Apr 30, 2015 · 4 comments
Closed

How can I open a prompt in a certain state? #61

uusijani opened this issue Apr 30, 2015 · 4 comments

Comments

@uusijani
Copy link

Sorry if this is obvious, but how can I open a multiple-state prompt in a specific state other than the first one in the states object? In other words, in the first statesdemo example, how can I open the prompt in state1 instead of state0?

I'm working on a login/logout/registration form with each of the three as a separate state and would like to start the prompt in either the "log in" or "log out" state depending on whether the user is currently logged in or not.

(I'm trying to work out the solution reading Impromptu's source code, but figured I'd also pose this question here for others like me to find in the future.)

@trentrichardson
Copy link
Owner

Hey @uusijani This is a very good question, and I there actually isn't directly a way to do this outside of making the first state in your array the one you open first. I can definitely see this being an option like "initialState" or something. This would take place around line 369 in the src folder:

var $firstState = t.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
t.goToState($firstState.data('jqi-name'));

So we might could change it to either look for a string for state name, or number for index (the current method)

t.goToState(
    isNaN(opts.initialState) ? opts.initialState : 
        t.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(opts.initialState).data('jqi-name')
);

The option would also need to be added the the defaults object and default to 0. You would then call it like:

$.prompt(states, { initialState: 'my-state-name' });

I will try to update the dev branch with this as I it would be a great addition. Thanks!

@uusijani
Copy link
Author

Hey @trentrichardson, and thanks for the response! The best I could figure in the meantime was to initialize the prompt differently in each branch, like so:

if (user.isSignedIn) {
    states = {
        signOut: states.signOut,
        signIn: states.signIn,
        register: states.register
    };
} else {
    states = {
        signIn: states.signIn,
        signOut: states.signOut,
        register: states.register
    };
}
thePrompt = $.prompt(states);

But it's definitely better to have true support for this in the extension itself. Excellent!

@trentrichardson
Copy link
Owner

@uusijani This was the first change to the dev branch since the last stable release, so it should be pretty stable if this change works. This change is located in the dev branch:

https://github.com/trentrichardson/jQuery-Impromptu/tree/dev

@uusijani
Copy link
Author

uusijani commented May 1, 2015

@trentrichardson I've just tested the branch with the change and it's working perfectly.

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