我知道有很多问题,我通过这些回答解决了第一个问题。我决定不对我的全局命名空间进行规范并使用这种类型的解决方案。但是现在我有一个与相同框架有关的不同问题。 我的模型就是这样开始的。
define([
'backbone',
'backbone.relational'
], function(Backbone){
var MenuNode = function () {
Backbone.RelationalModel.apply(this,arguments);
}
var NodeCollection = function() {
Backbone.Collection.apply(this,arguments);
}
MenuNode = Backbone.RelationalModel.extend({
constructor: MenuNode,
relations:[
{
type: Backbone.HasMany,
key: "children",
relatedModel: MenuNode,
collectionType: NodeCollection,
reverseRelation: {
key: "parent"
}
}
]
})
NodeCollection = Backbone.Collection.extend({
constructor: NodeCollection,
model: MenuNode,
url: function() {
return "/nodes"
}
})
这将为我的应用程序创建nessecary模型,这是一个jstree。 但我的问题是如何创建与api和我的关系的连接,以及如何使用骨干获得节点的当前子节点。 我有api:
nodes/ returns the root nodes in a simplified version
nodes/id returns the full info about node, with children and parent simplified
nodes/id/children returns the simplified version of the children of a specific node
但是如何通过骨干网获取特定节点的当前子节点? 我希望能够在我想要加载孩子时拨打电话,但不要预先加载孩子。我试图保存服务器的请求。因为有一个很大的树视图,意味着它的unessecary加载整个树。
只需提出任何问题即可澄清任何问题。 提前
答案 0 :(得分:0)
您应该能够通过使用字符串文字作为相关模型来完成此任务:
relations:[
{
type: Backbone.HasMany,
key: "children",
relatedModel: 'MenuNode',
collectionType: NodeCollection,
reverseRelation: {
key: "parent"
}
}
]
答案 1 :(得分:0)
我发现我的api非常奇怪。我有一个egde案例,我的jstree假设是由Backbone支持而不是jstree ajax本身。因为我必须根据骨干观看,我希望它们能够互相合作。
此修改已完成,但不在关系模型部分中。它是在我的后端,直接指向一个节点时为孩子们服务,这有助于我正确地建立模型。
所以它有效,但jstree不支持骨干