Using jQuery dynatree with Knockout and Breeze

前端 未结 1 1183
一生所求
一生所求 2021-01-28 20:51

OK, following the suggestion from PW Kad I\'m splitting this part of the question off from where it started on question ID 17973991.

I have a viewmodel that utilises a d

1条回答
  •  迷失自我
    2021-01-28 21:42

    You are not populating the treeMaterials because you don't have any data in materials when you are sending it to asTreeMaterials. I am making some assumptions here but basically it looks like this is what you are trying to do -

    At the top of your view model, I assume you have two observableArrays

    var treeMaterials = ko.observableArray();
    var materials = ko.observableArray();
    

    For your activate method, you need to go get some data, and then when your datacontext returns a promise, go make a tree out of it of some object type -

    var activate = function () {
        return datacontext.getMaterialPartials(materials).then(
                makeMyTree);
    };
    

    You don't need to pass treeMaterials or materials because they are within the scope of the view model already, and you are just trying to make a tree of objects out of your materials.

    var makeMyTree = function () {
        treeMaterials([]);
        ko.utils.arrayForEach(materials(), function (mat) {
            treeMaterials.push(new treeMaterial(mat));
        });
    };
    

    This is going to make an observableArray of objects with observable properties, meaning if you are passing them or trying to get their value you would need to use something like treeMaterials()[0].name().

    In case your dynatree doesn't take observables, or isn't playing well with them

    I am not sure how your dynatree or w/e works with observables, so here is a standard array of non-observable objects instead of an observable array -

    var treeMaterials = [];
    
    var makeMyTree = function () {
        treeMaterials[];
        ko.utils.arrayForEach(materials(), function (mat) {
            treeMaterials.push(new treeMaterial(mat));
        });
    };
    
    var treeMaterial = function (data) {
        var self = this;
    
        self.name = data.name;
        self.id = data.id;
        self.children = [];
    
        $.each(data.children, function (index, item) {
            self.children.push(new Person(item));
        });
    };
    

    0 讨论(0)
提交回复
热议问题