我正试图在我的模板中找到一个关于Ajax调用成功的图像......但我不能!
这是我的代码:
class AvatarView extends Backbone.View
template: JST["backbone/templates/twitter_avatar"]
initialize:(options) ->
@user = options.user
render: =>
@$el.html(@template({
view_id: @cid
avatar_url: @user.avatar() || "src/image_not_found.png"
}))
@$el.find(".avatar").on('error', @_reloadAvatar)
_reloadAvatar: =>
$.ajax
type: 'GET'
url: "/profiles/api/twitter/#{@user.social_network_id()}/avatar_url"
success: (response) =>
if response.success
avatar_url = response.avatar_url
else
avatar_url = 'https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png'
@$el.find(".avatar").attr('src', avatar_url) # Here is the problem
我调试时:
以下是模板:
<img src="<%= avatar_url %>" alt="avatar" class="avatar" id="avatar-image-<%= view_id %>">
我感觉像是在丢失我的DOM对象或类似的东西......
答案 0 :(得分:0)
我仍然不明白为什么它不起作用,但我使用事件参数解决了我的问题:
_reloadAvatar:(event) =>
$.ajax
type: 'GET'
url: "/profiles/api/twitter/#{@user.social_network_id()}/avatar_url"
success: (response) =>
if response.success
avatar_url = response.avatar_url
else
avatar_url = 'https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png'
@(event.target).attr('src', avatar_url)