开发者

Storing an arbitrary action to be executed upon completion of an action

The flow for my application, for non-logined users, looks like this:

1) non-logined user does a given action that requires login (arbitrary)
2) dialog appears for non-logined user and he logins without any refreshing of the page
3) collecting of additional information if it is new user
3) the given action should be executed 

How can i do so? I believe it can be done with care开发者_StackOverflow中文版fully structured callbacks. Part of the tricky part is that there are a range of potential actions


Store the action as a callback in a variable:

var callback = function() { alert ("You are logged in."); };

When a non-logged user attempts to execute this action, by clicking a button, or whatever, your application should pass the callback as an argument to the log-in function like this:

show_login_form(callback);

... where callback is what will be executed on a succesful login. Putting it all together:

    var callback = function() { alert ("You are logged in."); };
    if (loggedIn)
         callback();
    else
         show_login_form(callback);

Just be sure you do some kind of server side validation, as my example is simplistic and easily hacked.


Try looking at the taconite plugin for jQuery. There was a thread on this and Ruby, but it seems to have disappeared except from Google's cache.


You've tagged ruby-on-rails, so I guess this is going to be a rails app. Are you planning on implementing your own authentication and login system or are you going to use a gem/plugin. I ask because I wouldn't have thought the range of actions would be the tricky part if the actions were all defined in the/a controller. For example with devise adding:

before_filter :authenticate_user!

in the controller would ensure that all the actions would require a login. This can be refined further using :except like

before_filter :authenticate_user!, :except => [:index, :show]

With that in mind we only really need to think about the ajax parts. You could perhaps achieve this using js.erb templates and using respond_to in the method, for example

def log_in
   respond_to do |f|
     # gracefully fallback!
     f.html { redirect_to new_user_session_path }
     # or do something snazy..
     f.js
   end
end

The hard part might actually be getting the ajax log in part working, but hopefully what I've said might help the thought process.

Ryan Bates has done a good screencast on jQuery with Rails but I can't post the link...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜