Question

I know there are multiple questions about this and I have solved the first problem through those answeres. I decided to not polute my global namespace and use this type of solution. But now I have a different problem relating the same frameworks. My models start like this.

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"
    }
})

This will create the models nessecary for my application, which is a jstree. But my question is how I create the connection with the api and my relations, and how I get the current children of a node using backbone. I have the 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

But how can I get the current children of a specific node through backbone? I want to be able to make a call when i want to load the children, but not preload the children. Im trying to save the server for requests. Because have a big tree view, means that its unessecary to load the whole tree.

Just ask any questions to clarify anything more. Thx ahead

Was it helpful?

Solution 2

I figured out that my api was just very strange. I had an egde case where my jstree was suppose to be supported by Backbone and not the jstree ajax itself. Because I had to views depending on backbone and i wanted them to work with eachother.

This modification has been done, but it was not in the relation model part. It was in my backend and serving the children when pointing directly at a node, which helps me building the model correctly.

So it works, but jstree does not have support for backbone

OTHER TIPS

You should be able to accomplish this by using a string literal as related model:

relations:[
    {
        type: Backbone.HasMany,
        key: "children",
        relatedModel: 'MenuNode',
        collectionType: NodeCollection,
        reverseRelation: {
            key: "parent"
        }
    }
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top