Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
add test for 'share message to friend', remove old facebook_share_wid…
Browse files Browse the repository at this point in the history
…get.js file
  • Loading branch information
fuying committed Aug 21, 2012
1 parent fe8a345 commit af4861e
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 207 deletions.
Expand Up @@ -4,4 +4,13 @@
# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
# the compiled file.
#= require_tree .
#
#

window.app =
models: {}
views: {}

window.facebookShareWidget =
callbacks:
success: (friend) ->
fail: (friend) ->
@@ -0,0 +1,46 @@
#= require underscore.js
#= require backbone.js
#= require handlebars-runtime.js

$ ->
class FacebookFriend extends Backbone.Model
url: '/widget/facebook/share'

isShared: ->
@get('status') == 'shared'

shared: ->
@set('status': 'shared')

isSharing: ->
@get('status') == 'sharing'

startSharing: ->
@set('status': 'sharing')

shareFailedBecause: (reason)->
@set('status': 'failed', 'reason': reason)

isShareFailed: ->
@get('status') == 'failed'

share: (template) ->
friend = this
data = $.extend({}, $.parseJSON(template))
data.facebook_id = @id
data.message = @messageModel.content()
@startSharing()
$.ajax
url: @url
type: "post"
data: { post: data }
success: (resp) =>
friend.shared()
error: (jqXHR, textStatus, errorThrown) =>
json = $.parseJSON(jqXHR.responseText)
friend.shareFailedBecause(json.message)

setMessageModel: (model) ->
@messageModel = model

app.models.FacebookFriend = FacebookFriend
@@ -0,0 +1,50 @@
#= require underscore.js
#= require backbone.js
#= require handlebars-runtime.js

$ ->
class FacebookFriendView extends Backbone.View
tagName: 'li'
events:
'click.share-button': 'shareToFriend'

initialize: ->
@model.on('change', @render, this)

render: ->
friend = @model.toJSON()
$(@el).html(HandlebarsTemplates['facebook_share_widget/templates/friend'](friend))
if @model.isShared()
@renderShared()
else if @model.isSharing()
@renderSharing()
else if @model.isShareFailed()
@renderFailed(friend)
else
@renderToShare(friend)
this

renderShared: ->
$(@el).append("<div class='indicator'><img src='#{$('.tick img').attr('src')}'/> Shared</div>")
this

renderSharing: ->
$(@el).append("<div class='indicator'><img src='#{$('.loader img').attr('src')}'/> Sharing</div>")
this

renderToShare: (friend) ->
$(@el).append("<a class='share-button' data-facebook-id='#{friend.id}'>Share</a>")
this

renderFailed: (friend) ->
$(@el).children('.error-message').text(friend.reason)
this

shareToFriend: (event) ->
event.preventDefault()
@model.share(@sharingTemplate())

sharingTemplate: ->
$('.template').text()

app.views.FacebookFriendView = FacebookFriendView
@@ -1,52 +1,9 @@
#= require underscore.js
#= require backbone.js
#= require handlebars-runtime.js
#= require facebook_share_widget/sharing_message

$ ->
class FacebookFriend extends Backbone.Model
url: '/widget/facebook/share'

isShared: ->
@get('status') == 'shared'

shared: ->
@set('status': 'shared')

isSharing: ->
@get('status') == 'sharing'

startSharing: ->
@set('status': 'sharing')

shareFailedBecause: (reason)->
@set('status': 'failed', 'reason': reason)

isShareFailed: ->
@get('status') == 'failed'

share: (template) ->
friend = this
data = $.extend({}, $.parseJSON(template))
data.facebook_id = @id
data.message = @messageModel.content()
@startSharing()
$.ajax
url: @url
type: "post"
data: { post: data }
success: (resp) =>
friend.shared()
error: (jqXHR, textStatus, errorThrown) =>
json = $.parseJSON(jqXHR.responseText)
friend.shareFailedBecause(json.message)

setMessageModel: (model) ->
@messageModel = model

class SharingMessage extends Backbone.Model
content: ->
@get('msg')

class NameSearch extends Backbone.Model
searchFilter: ->
@get('filter')
Expand All @@ -56,7 +13,7 @@ $ ->

class FacebookFriends extends Backbone.Collection
url: '/widget/facebook/friends'
model: FacebookFriend
model: app.models.FacebookFriend

filterBy: (criteria) ->
unless criteria
Expand Down Expand Up @@ -94,53 +51,9 @@ $ ->
filterUpdate: ->
@model.setSearchFilter($('input.search-text').val())

class FacebookFriendView extends Backbone.View
tagName: 'li'
events:
'click.share-button': 'shareToFriend'

initialize: ->
@model.on('change', @render, this)

render: ->
friend = @model.toJSON()
$(@el).html(HandlebarsTemplates['facebook_share_widget/templates/friend'](friend))
if @model.isShared()
@renderShared()
else if @model.isSharing()
@renderSharing()
else if @model.isShareFailed()
@renderFailed(friend)
else
@renderToShare(friend)
this

renderShared: ->
$(@el).append("<div class='indicator'><img src='<%= asset_path("facebook_share_widget/tick.png") %>'/> Shared</div>")
this

renderSharing: ->
$(@el).append("<div class='indicator'><img src='<%= asset_path("facebook_share_widget/loader.gif") %>'/> Sharing</div>")
this

renderToShare: (friend) ->
$(@el).append("<a class='share-button' data-facebook-id='#{friend.id}'>Share</a>")
this

renderFailed: (friend) ->
$(@el).children('.error-message').text(friend.reason)
this

shareToFriend: (event) ->
event.preventDefault()
@model.share(@sharingTemplate())

sharingTemplate: ->
$('.template').text()

class FacebookFriendsView extends Backbone.View
initialize: ->
@sharingMessage = new SharingMessage
@sharingMessage = new app.models.SharingMessage
@sharingMessageView = new SharingMessageView(el: $('.message-pane'), model: @sharingMessage)
@sharingMessageView.render()

Expand All @@ -166,8 +79,9 @@ $ ->
@appendFriend friend for friend in filteredFriends

appendFriend: (friend) ->
friendView = new FacebookFriendView(model: friend)
friendView = new app.views.FacebookFriendView(model: friend)
@$('#friends').append(friendView.render().el)

if $('#container').length
new FacebookFriendsView(el: $('#container'))
new FacebookFriendsView(el: $('#container'))

This file was deleted.

@@ -0,0 +1,5 @@
$ ->
class SharingMessage extends Backbone.Model
content: ->
@get('msg')
app.models.SharingMessage = SharingMessage
6 changes: 6 additions & 0 deletions app/views/facebook_share_widget/facebook/_widget.html.haml
Expand Up @@ -5,6 +5,12 @@

.template.hide
= options[:template].to_json
.tick.hide
= image_tag "facebook_share_widget/tick.png"
.loader.hide
= image_tag "facebook_share_widget/loader.gif"
.logo.hide
= image_tag "facebook_share_widget/facebook-logo.png"

#container
= image_tag 'facebook_share_widget/loader.gif'

0 comments on commit af4861e

Please sign in to comment.