2016-01-06 87 views
0

以下coffeeScript正常運行沒有setUpdateInterval的引用。通知的設置間隔

class Notifications 
    constructor: -> 
    @notifications = $("[data-behavior='notifications']") 
    @setup() if @notifications.length > 0 
    setUpdateInterval() 

    setup: -> 
    $.ajax(
     url: "/notifications.json" 
     dataType: "JSON" 
     method: "GET" 
     success: @handleSuccess 
    ) 

    handleSuccess: (data) => 
    items = $.map data, (notification) -> 
     "<li class='active'><a href='#{notification.url}'>#{notification.actor} #{notification.notifiable.type}</a></li>" 

    $("[data-behavior='unread-count']").text(items.length) 
    $("[data-behavior='notification-items']").html(items) 

    setUpdateInterval: (notifications) -> 
     callback = @setup.bind(this) 
     setInterval(callback, 15000) 

jQuery -> 
    new Notifications 

什麼是不正確的插入額外的行和集團?

+0

'setUpdateInterval' *在*'handleSuccess'內,所以'handleSuccess'返回'{setUpdateInterval:function ....}'。另外,'setUpdateInterval()'試圖調用一個全局函數,'@setUpdateInterval()'會調用該方法(當然如果有這種方法的話)。 –

回答

0

根據評論,下列功能。

constructor: -> 
    @notifications = $("[data-behavior='notifications']") 
    @setUpdateInterval() 
    @setup() if @notifications.length > 0 

    setup: -> 
    $.ajax(
     url: "/notifications.json" 
     dataType: "JSON" 
     method: "GET" 
     success: @handleSuccess 
    ) 

    setUpdateInterval: (notifications) -> 
     callback = @setup.bind(this) 
     setInterval(callback, 15000)