Sorting
This commit is contained in:
@@ -6,6 +6,7 @@ if Backbone?
|
||||
DiscussionUtil.addDiscussion @id, @
|
||||
@bind "add", (item) =>
|
||||
item.discussion = @
|
||||
@comparator = @sortByDate
|
||||
|
||||
find: (id) ->
|
||||
_.first @where(id: id)
|
||||
@@ -16,6 +17,19 @@ if Backbone?
|
||||
@add model
|
||||
model
|
||||
|
||||
sortByDate: (thread) ->
|
||||
thread.get("created_at")
|
||||
|
||||
sortByVotes: (thread1, thread2) ->
|
||||
thread1_count = parseInt(thread1.get("votes")['up_count'])
|
||||
thread2_count = parseInt(thread2.get("votes")['up_count'])
|
||||
thread2_count - thread1_count
|
||||
|
||||
sortByComments: (thread1, thread2) ->
|
||||
thread1_count = parseInt(thread1.get("comments_count"))
|
||||
thread2_count = parseInt(thread2.get("comments_count"))
|
||||
thread2_count - thread1_count
|
||||
|
||||
class @DiscussionView extends Backbone.View
|
||||
|
||||
$: (selector) ->
|
||||
|
||||
@@ -8,7 +8,6 @@ class @DiscussionRouter extends Backbone.Router
|
||||
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".sidebar"))
|
||||
@nav.on "thread:selected", @navigateToThread
|
||||
@nav.render()
|
||||
@main = new DiscussionThreadView(el: $(".discussion-column"))
|
||||
|
||||
allThreads: ->
|
||||
true
|
||||
@@ -16,7 +15,10 @@ class @DiscussionRouter extends Backbone.Router
|
||||
showThread: (forum_name, thread_id) ->
|
||||
@nav.setActiveThread(thread_id)
|
||||
thread = @discussion.get(thread_id)
|
||||
@main.model = thread
|
||||
if(@main)
|
||||
@main.undelegateEvents()
|
||||
|
||||
@main = new DiscussionThreadView(el: $(".discussion-column"), model: thread)
|
||||
@main.render()
|
||||
|
||||
navigateToThread: (thread_id) =>
|
||||
|
||||
@@ -3,6 +3,7 @@ class @DiscussionThreadListView extends Backbone.View
|
||||
events:
|
||||
"click .search": "showSearch"
|
||||
"keyup .post-search-field": "performSearch"
|
||||
"click .sort-bar a": "sortThreads"
|
||||
|
||||
render: ->
|
||||
@timer = 0;
|
||||
@@ -26,7 +27,7 @@ class @DiscussionThreadListView extends Backbone.View
|
||||
@trigger("thread:selected", thread_id)
|
||||
|
||||
setActiveThread: (thread_id) ->
|
||||
@$("a").removeClass("active")
|
||||
@$(".post-list a").removeClass("active")
|
||||
@$("a[data-id='#{thread_id}']").addClass("active")
|
||||
|
||||
showSearch: ->
|
||||
@@ -34,6 +35,20 @@ class @DiscussionThreadListView extends Backbone.View
|
||||
@$(".browse").removeClass('is-open');
|
||||
setTimeout (-> @$(".post-search-field").focus()), 200
|
||||
|
||||
sortThreads: (event) ->
|
||||
@$(".sort-bar a").removeClass("active")
|
||||
$(event.target).addClass("active")
|
||||
sortBy = $(event.target).data("sort")
|
||||
if sortBy == "date"
|
||||
@collection.comparator = @collection.sortByDate
|
||||
else if sortBy == "votes"
|
||||
@collection.comparator = @collection.sortByVotes
|
||||
else if sortBy == "comments"
|
||||
@collection.comparator = @collection.sortByComments
|
||||
@collection.sort()
|
||||
|
||||
|
||||
|
||||
delay: (callback, ms) =>
|
||||
clearTimeout(@timer)
|
||||
@timer = setTimeout(callback, ms)
|
||||
|
||||
@@ -18,12 +18,17 @@
|
||||
$$contents = {};
|
||||
$$discussions = {};
|
||||
$$course_id = "${course_id}";
|
||||
$(document).ready(function() {
|
||||
DiscussionApp = {
|
||||
start: function() {
|
||||
window.user = new DiscussionUser(JSON.parse("${user_info | escapejs}"));
|
||||
var discussion = new Discussion(JSON.parse("${threads | escapejs}"));
|
||||
|
||||
new DiscussionRouter({discussion: discussion});
|
||||
Backbone.history.start({pushState: true, root: "/courses/${course_id}/discussion/forum/"});
|
||||
}
|
||||
}
|
||||
$(document).ready(function() {
|
||||
DiscussionApp.start();
|
||||
});
|
||||
</script>
|
||||
</%block>
|
||||
|
||||
Reference in New Issue
Block a user