문제

What's a good way to display a star that the user can click on/off from a webpage similar to the "mark important" star in gmail? Also, similar to the star in stackoverflow (over here to your left!)

I'm using jQuery and rails.

FRONT-side questions: What's a good and clean way to maintain this in jQuery? Maybe have a mark important and then add a click listener perhaps? I'll also need to remember the state and show the star highlighted on and off.

SERVER-side questions: The other question I have is about routing. When the user clicks on it, it'll POST to the server.

Should it have two routes?

POST ./email/mark_important
POST ./email/unmark_important

Or should it have or one route?

POST ./email/toggle_importance

I'm focused on clean code & maintainability. Thanks!

도움이 되었습니까?

해결책

For the client-side, I'd use a toggle on the css class (for instance "checked" and "unchecked"). After the toggle took place, I'd read the resulting CSS from the element, and call a unique server-side method with an on/off param (a primitive boolean) to save the sate.

This way, you always sends the right value to your server, even if the user clicks a lot of time (and very fast) on the image. The last call will always make things right.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top