获取 Collection 和解析其关联模型之间的关系
Relationship between Fetching a Collection and Parsing its Associated Model
当在 BackboneJS collection 上调用 fetch()
时,为什么以及如何调用模型中的 parse()
?我基本上有 Board collection 和模型,还有 List collection 和模型。一个列表belongs-to一个板。我想使用 Board 模型将 List 作为 JSON 数据发送(当调用 "GET" 请求时,例如);但是,我不想在获取 collection 时将 List 作为 JSON 数据发送。
我的理解是,在 collection 上调用 fetch 会经历以下步骤(永远不会通过模型,更不用说模型上的 parse()
):
index
action in controller -> sends JSON/data to Backbone -> the collection receives this data and stores it
我有 collection 个电路板,其中有一个关联模型:
棋盘Collection:
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;
}
}
});
基本上,对于特定的 Board 模型,我将 "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
对于 Board collection,另一方面,我不会发回 "list":
def index
@boards = current_user.boards
render json: @boards
end
我上面实现的问题是,当我fetch()
Boardcollection时,每个Board的属性object没有传送过来。但是当我注释掉 parse()
函数时,一切正常。
编辑:
我想通了为什么我没有在 collection 提取中获取数据。我忘了在 parse
函数的末尾添加 return response
。如果有人可以阐明在获取 collection 时发生的步骤序列到底是什么(在此序列中进行解析),那就太好了。谢谢!
查看 Backbone 的注释来源。
获取数据时 Backbone 设置 {reset: false}
或重置 {reset: true}
集合值。然后它调用你的成功回调并最终触发 sync
:
http://backbonejs.org/docs/backbone.html#section-133
在 collection.set
中,您可以准确地看到 model.parse
方法何时被调用以及为什么必须 return 响应:
当在 BackboneJS collection 上调用 fetch()
时,为什么以及如何调用模型中的 parse()
?我基本上有 Board collection 和模型,还有 List collection 和模型。一个列表belongs-to一个板。我想使用 Board 模型将 List 作为 JSON 数据发送(当调用 "GET" 请求时,例如);但是,我不想在获取 collection 时将 List 作为 JSON 数据发送。
我的理解是,在 collection 上调用 fetch 会经历以下步骤(永远不会通过模型,更不用说模型上的 parse()
):
index
action in controller -> sends JSON/data to Backbone -> the collection receives this data and stores it
我有 collection 个电路板,其中有一个关联模型:
棋盘Collection:
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;
}
}
});
基本上,对于特定的 Board 模型,我将 "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
对于 Board collection,另一方面,我不会发回 "list":
def index
@boards = current_user.boards
render json: @boards
end
我上面实现的问题是,当我fetch()
Boardcollection时,每个Board的属性object没有传送过来。但是当我注释掉 parse()
函数时,一切正常。
编辑:
我想通了为什么我没有在 collection 提取中获取数据。我忘了在 parse
函数的末尾添加 return response
。如果有人可以阐明在获取 collection 时发生的步骤序列到底是什么(在此序列中进行解析),那就太好了。谢谢!
查看 Backbone 的注释来源。
获取数据时 Backbone 设置 {reset: false}
或重置 {reset: true}
集合值。然后它调用你的成功回调并最终触发 sync
:
http://backbonejs.org/docs/backbone.html#section-133
在 collection.set
中,您可以准确地看到 model.parse
方法何时被调用以及为什么必须 return 响应: