mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-04-29 05:05:25 +08:00
JS VisibleTo module
This commit is contained in:
@@ -1 +1 @@
|
||||
@Campo = {}
|
||||
@campo = {}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
@Posts =
|
||||
@campo.Posts =
|
||||
init: ->
|
||||
@bindActions()
|
||||
|
||||
@@ -34,4 +34,4 @@
|
||||
else
|
||||
votes.text('').removeClass('up down')
|
||||
|
||||
@Posts.init()
|
||||
@campo.Posts.init()
|
||||
|
||||
11
app/assets/javascripts/visible_to.js.coffee
Normal file
11
app/assets/javascripts/visible_to.js.coffee
Normal file
@@ -0,0 +1,11 @@
|
||||
@campo.VisibleTo =
|
||||
# data-visible-to="options"
|
||||
#
|
||||
# options:
|
||||
# - user: visible to logined user
|
||||
check: ->
|
||||
if !window.campo.currentUser?
|
||||
$('[data-visible-to~=user]').remove()
|
||||
|
||||
$ ->
|
||||
campo.VisibleTo.check()
|
||||
@@ -7,6 +7,11 @@ html
|
||||
= javascript_include_tag "application", "data-turbolinks-track" => true
|
||||
meta name="viewport" content="width=device-width, initial-scale=1.0"
|
||||
= csrf_meta_tags
|
||||
- if logined?
|
||||
javascript:
|
||||
campo.currentUser = {
|
||||
id: #{current_user.id}
|
||||
};
|
||||
|
||||
body
|
||||
nav.navbar.navbar-campo.navbar-static-top
|
||||
|
||||
@@ -10,21 +10,20 @@ li.list-group-item.list-group-item-campo.post id="post_#{post.id}" data-post-id=
|
||||
= time_ago_in_words post.created_at
|
||||
article.text
|
||||
= format_post post.content
|
||||
- if logined?
|
||||
.actions.clearfix
|
||||
- if current_user == post.user
|
||||
.pull-left
|
||||
- if post.post_number == 1
|
||||
a.btn href=edit_topic_path(post.topic)
|
||||
span.glyphicon.glyphicon-edit
|
||||
- else
|
||||
a.btn href=edit_post_path(post) data-remote='true'
|
||||
span.glyphicon.glyphicon-edit
|
||||
.pull-right
|
||||
span.votes class=(post.votes > 0 ? 'up' : 'down')
|
||||
- if post.votes != 0
|
||||
= post.votes
|
||||
a.btn data-post-vote-action="up"
|
||||
span.glyphicon.glyphicon-thumbs-up
|
||||
a.btn data-post-vote-action="down"
|
||||
span.glyphicon.glyphicon-thumbs-down
|
||||
.actions.clearfix
|
||||
- if current_user == post.user
|
||||
.pull-left
|
||||
- if post.post_number == 1
|
||||
a.btn href=edit_topic_path(post.topic)
|
||||
span.glyphicon.glyphicon-edit
|
||||
- else
|
||||
a.btn href=edit_post_path(post) data-remote='true'
|
||||
span.glyphicon.glyphicon-edit
|
||||
.pull-right
|
||||
span.votes class=(post.votes > 0 ? 'up' : 'down')
|
||||
- if post.votes != 0
|
||||
= post.votes
|
||||
a.btn data-post-vote-action="up"
|
||||
span.glyphicon.glyphicon-thumbs-up
|
||||
a.btn data-post-vote-action="down"
|
||||
span.glyphicon.glyphicon-thumbs-down
|
||||
|
||||
@@ -10,7 +10,7 @@ test 'should update votes', ->
|
||||
</ul
|
||||
")
|
||||
|
||||
Posts.updateVotes([
|
||||
campo.Posts.updateVotes([
|
||||
{ post_id: 1, value: 'up' }
|
||||
{ post_id: 2, value: 'down' }
|
||||
])
|
||||
|
||||
25
test/javascripts/visible_to_test.js.coffee
Normal file
25
test/javascripts/visible_to_test.js.coffee
Normal file
@@ -0,0 +1,25 @@
|
||||
module('visible-to')
|
||||
|
||||
test 'should keep element for user if logined', ->
|
||||
$fixture = $('#qunit-fixture')
|
||||
$fixture.append("
|
||||
<div data-visible-to='user'></div>
|
||||
")
|
||||
|
||||
campo.currentUser = { id: 1 }
|
||||
campo.VisibleTo.check()
|
||||
|
||||
ok($fixture.find('div').length)
|
||||
|
||||
# clear
|
||||
campo.currentUser = null
|
||||
|
||||
test 'should remove element for user if logined', ->
|
||||
$fixture = $('#qunit-fixture')
|
||||
$fixture.append("
|
||||
<div data-visible-to='user'></div>
|
||||
")
|
||||
|
||||
campo.VisibleTo.check()
|
||||
|
||||
ok(!$fixture.find('div').length)
|
||||
Reference in New Issue
Block a user