When to add extend additional complex types onto a Breeze entity

[亡魂溺海] 提交于 2019-12-11 13:51:43

问题


I want to load an entity from the database, and then using it's ICollection (from the model class) load up some child data. This would be simple enough to do from individual view models if it was a 1 to many relationship, but I have a bit more complex structure -

Parent has many children. Each child has many grandchildren, that need to be linked back to the proper child. The hierarchy needs to remain in tact.

The other options that I have come up with so far may not be the best way so I my question is - what is the best practice to load up the grandchildren - or some other method?

in a constructor while configuring the metadataStore -

function configureMetadataStore(metadataStore) {
    metadataStore.registerEntityTypeCtor(
        'Child', null, childInitializer);
}

function childInitializer(child) {
    child.grandchildren = (Do something here)
        return grandchildren;
    });
}

In the viewmodel where children are being populated -

function refresh() {
    return datacontext.getChildren(childs, parentId);
}

var addGrandChildren = function () {
    $.each(childs, function (i) {
        var grandChildren = ko.observableArray();
        var childId = $(this).data(id);
        datacontext.getGrandChildren(grandChildren, childId);
    });
    return;
};

Or some other method?


回答1:


Providing that your relationships are not unidirectional, Breeze entities will automatically hook themselves together when you query them. ( Edit: as of v 1.3.5 - breeze will also hook up unidirectional relations as well. )

This means that if you use a query to extract n entities that just happen to be related all of them will be automatically linked to one another in the correct fashion. The same occurs if you use the EntityQuery.expand method. So your issue is simply how to query for whatever portion of the graph you want in the least number of calls.

Note: you should also look at the EntityAspect.loadNavigationProperty method if you really want to actually "walk" the graph. But this can be non-performant if you are dealing with large graphs.




回答2:


I have the same issue with Breezejs (1.4.2) q (0.9.7) I want to add a computed property for an entity.

var doctorInitializer = function (doctor) {
    doctor.FullName = ko.computed(function () {           
        return doctor.FirstName() + " " + doctor.MiddleName() + " " + doctor.LastName() + " " + doctor.SurName();           
    });
};

var doctorName = '/breeze/polyclinic', 
doctorManager = new breeze.EntityManager(doctorName);
var store = doctorManager.metadataStore;
store.registerEntityTypeCtor("Doctor", null, doctorInitializer);

i try adding a knockout computed to the constructor

var doctor = function () {
  self.FullName = ko.computed( {
    read: function() {
       return self.FirstName + " " + self.MiddleName + " " + self.LastName + " " + self.SurName;
    },
    deferEvaluation: true
  });
};
store.registerEntityTypeCtor("Doctor", doctorInitializer);

in both cases only work if i remove the parenthesis but MiddleName and SurName is not required and instead of empty string i got null

this is the error i have http://screencast.com/t/bP9Xnmf9Jm



来源:https://stackoverflow.com/questions/16196861/when-to-add-extend-additional-complex-types-onto-a-breeze-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!