2

I'm using jsTree plugin to build tree of custom items. I know how to load items via ajax requests, but in my case I have nodes in variable m_items which is Array type. When node is expanding I need to return only part of items from m_items, so I need some way to load subnodes with custom function which accepts expandable node and which will return a array of nodes.

I think that there is at least one way to resolve it, but it is a custom way: initialy loads root level and one level of each subnode. When any node is opened, then loads next level of node. But this way has at least one leak: loads too much unnecessary items. So I'm searching some built-in functionality.

1 Answer 1

2

It's an interesting problem, however if your target browsers are up to date browsers, excluding < IE9, this should work :

var TreeHelper = function(items, limit, start) {
    this.items_ = items || [];
    this.limit = limit || 5;
    this.start = start || 0;
}

TreeHelper.prototype = {
    push: function(item) {
        this.items_.push(item);
        return this;
    }
};

Object.defineProperty(TreeHelper.prototype, 'items', {
    get: function() {
        return this.items_.slice(this.start, this.limit);
    }
});

var helper = new TreeHelper([item1, item2, item3, ...], 5, 0);
......
function_that_needs_m_Items(helper.items);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.