Commits

Anonymous committed ec9080c

js refactoring

  • Participants
  • Parent commits 535ad78

Comments (0)

Files changed (6)

File newsmeme/models.py

                  Post.PUBLIC : "public",
                  Post.FRIENDS : "friends",
                  Post.PRIVATE : "private"
-               }.setdefault(self.access)
+               }.get(self.access, "public")
         
     def can_access(self, user=None):
         if self.access == self.PUBLIC:

File newsmeme/static/js/newsmeme.js

-var newsmeme = function(){}
+var newsmeme = {
 
+    ajax_post : function(url, params, on_success){
+    
+        var _callback = function(response){
+            if (response.success) {
+                if (response.redirect_url){
+                    window.location.href = response.redirect_url;
+                } else if (response.reload){
+                    window.location.reload(); 
+                } else if (on_success) {
+                    return on_success(response);
+                }
+            } else  {
+                return newsmeme.message(response.error, "error");
+            }
+        }
 
-newsmeme.ajax_post = function(url, params, on_success, content_type){
+        $.post(url, params, _callback, "json");
+
+    },
     
-    var _callback = function(response){
-        if (response.success) {
-            return on_success(response);
-        } else  {
-            return newsmeme.fail(response.error);
+    message : function(message, category){
+        $('ul#messages').html('<li class="' + category + '">' + message + '</li>').fadeOut();
+    },
+
+    delete_comment : function(url) {
+        var callback = function(response){
+            $('#comment-' + response.comment_id).fadeOut();
+        }   
+        newsmeme.ajax_post(url, null, callback);
+    },
+
+    vote_post : function(url){
+        var callback = function(response){
+            $('#vote-' + response.post_id).hide();
+            $('#score-' + response.post_id).text(response.score);
         }
+
+        newsmeme.ajax_post(url, null, callback);
+    },
+
+
+    vote_comment : function(url){
+        var callback = function(response){
+            $('#vote-comment-' + response.comment_id).hide();
+            $('#score-comment-' + response.comment_id).text(response.score);
+        }
+
+        newsmeme.ajax_post(url, null, callback);
     }
 
-    $.post(url, params, _callback, "json");
-
 }
-
-newsmeme.fail = function(error){
-    $('#js_error').html(msg).fadeOut('slow');
-}
-
-newsmeme.follow = function(url){
-    var callback = function(response){
-        $('#follow').hide();
-        $('#unfollow').show();
-        $('#followers').text(response.followers);
-    }
-
-    newsmeme.ajax_post(url, null, callback);
-}
-
-newsmeme.unfollow = function(url){
-    var callback = function(response){
-        $('#unfollow').hide();
-        $('#send_message').hide();
-        $('#follow').show();
-        $('#followers').text(response.followers);
-    }
-
-    newsmeme.ajax_post(url, null, callback);
-}
-
-newsmeme.delete_post = function(url) {
-    var callback = function(response){
-        window.location.href = response.redirect_url;
-    }
-    newsmeme.ajax_post(url, null, callback);
-}
-
-newsmeme.delete_comment = function(url) {
-    var callback = function(response){
-        $('#comment-' + response.comment_id).fadeOut();
-    }   
-    newsmeme.ajax_post(url, null, callback);
-}
-
-newsmeme.vote_post = function(url){
-    var callback = function(response){
-        $('#vote-' + response.post_id).hide();
-        $('#score-' + response.post_id).text(response.score);
-    }
-
-    newsmeme.ajax_post(url, null, callback);
-}
-
-
-newsmeme.vote_comment = function(url){
-    var callback = function(response){
-        $('#vote-comment-' + response.comment_id).hide();
-        $('#score-comment-' + response.comment_id).text(response.score);
-    }
-
-    newsmeme.ajax_post(url, null, callback);
-}

File newsmeme/templates/base.html

             <li class="{{ status }}">{{ message }}</li>
             {% endfor %}
         </ul>   
+        {% else %}
+        <ul class="messages" style="display:none;"></ul>
         {% endif %}
-        <div class="error span-24 last" id="js_error" style="display:none;"></div>
 
         {% block content %}
 

File newsmeme/templates/post/post.html

     | <a href="#" onclick="$('#delete-post').toggle(); return false;">delete this post</a> 
     <div id="delete-post" style="display:none;">
         <strong>{{ _('Are you sure you want to delete this post ?') }}</strong>
-        <a href="#" onclick="newsmeme.delete_post('{{ url_for('post.delete', post_id=post.id) }}'); return false;">{{ _('yes') }}</a> / 
+        <a href="#" onclick="newsmeme.ajax_post('{{ url_for('post.delete', post_id=post.id) }}'); return false;">{{ _('yes') }}</a> / 
         <a href="#" onclick="$('#delete-post').toggle(); return false;">{{ _('no') }}</a>
     </div>
     {% endif %}

File newsmeme/templates/user/layout.html

     <a href="{{ url_for("account.edit") }}">edit your account</a> 
     {% elif g.user %}
     <span id="follow" {% if g.user.is_following(user) %} style="display:none;"{% endif %}>
-        <a href="#" onclick="newsmeme.follow('{{ url_for("account.follow", user_id=user.id) }}'); return false;">{{ _("follow") }} {{ user.username }}</a>
+        <a href="#" onclick="newsmeme.ajax_post('{{ url_for("account.follow", user_id=user.id) }}'); return false;">{{ _("follow") }} {{ user.username }}</a>
     </span>
     <span id="unfollow" {% if not g.user.is_following(user) %} style="display:none;"{% endif %}>
-        <a href="#" onclick="newsmeme.unfollow('{{ url_for("account.unfollow", user_id=user.id) }}'); return false;">{{ _("unfollow") }} {{ user.username }}</a>
+        <a href="#" onclick="newsmeme.ajax_post('{{ url_for("account.unfollow", user_id=user.id) }}'); return false;">{{ _("unfollow") }} {{ user.username }}</a>
     </span>
     {% if user.can_receive_mail(g.user) %}
     | <a id="send_message" href="{{ url_for('user.send_message', user_id=user.id) }}">{{ _("send message to %(name)s", name=user.username) }}</a>

File newsmeme/views/account.py

                       recipients=[user.email])
 
     return jsonify(success=True,
-                   followers=len(user.followers))
+                   reload=True)
 
 
 @account.route("/unfollow/<int:user_id>/", methods=("POST",))
     db.session.commit()
 
     return jsonify(success=True,
-                   followers=len(user.followers))
+                   reload=True)