2015-06-27 61 views
0

當在BackboneJS集合上調用fetch()時,模型中的parse()爲什麼以及如何被調用?我基本上董事會收集和型號,也列表採集和模型。一個列表屬於-到。我想送列表下來,用模型JSON數據(當「GET」請求調用,例如);但是,我不想在獲取集合時將列表作爲JSON數據向下發送。獲取集合並解析其關聯模型之間的關係

我的理解是,調用獲取對集合經過以下步驟(通過模型永遠不會,更不用說parse()型號):

index動作控制器 - >發送JSON /數據骨幹 - >收集接收該數據並將其存儲

我有在該基板的一個集合具有相關聯的模型:

收藏

TrelloClone.Collections.Boards = Backbone.Collection.extend({ 
    url: '/api/boards', 

    model: TrelloClone.Models.Board 
}); 

TrelloClone.Collections.boards = new TrelloClone.Collections.Boards(); 

型號

TrelloClone.Models.Board = Backbone.Model.extend({ 
    urlRoot: '/api/boards', 

    lists: function() { 
    if(!this._lists) { 
     this._lists = new TrelloClone.Collections.Lists([], {board: this}); 
    } 
    return this._lists; 
    }, 

    parse: function(response) { 
    console.log("parsing"); 
    if(response.lists) { 
     console.log("inside lists"); 
     this.lists().set(response.lists); 
     delete response.lists; 
    } 
    } 
}); 

從本質上講,對於特定的模型,我送回去 「名單」 與董事會:

#in the boards controller 
def show 
    @board = Board.includes(:members, lists: :cards).find(params[:id]) 

    if @board.is_member?(current_user) 
    render :show 
    else 
    render json: ["You aren't a member of this board"], status: 403 
    end 
end 

... 

#in the JBuilder file... 
json.extract! @board, :title 

json.lists @board.lists do |list| 
    json.extract! list, :title, :ord 

    json.cards list.cards do |card| 
     json.extract! card, :title, :done 
    end 
end 

對於集合,在另一方面,我不回送「清單」:

def index 
    @boards = current_user.boards 
    render json: @boards 
end 

與我上面實現的問題是,當我fetch()收集,每個Board對象的屬性都不會被髮送。但是當我註釋掉parse()函數時,一切正常。

編輯: 我想通了,爲什麼我沒有得到一個集合取數據。在parse函數的末尾忘了return response。如果有人能夠澄清當一個集合被提取時發生的步驟順序(在這個順序中解析發生的地方),那將會很好。謝謝!

回答