How do I add a property to a breeze entity in the ViewModel, to assist with View display?

StackOverflow https://stackoverflow.com/questions/23398832

  •  13-07-2023
  •  | 
  •  

Вопрос

How do I add a knockoutobserable property to a breeze entity - IsSelected(), in the ViewModel, to assist with View display?

IsSelected is not a field in the entity nor should it be, however I need to flag entites as selected for UI DOM manipulation (adding/removing css classes, etc.)

IsSelected is really a UI thing, but this is where the MVVM pattern gets cloudy for me.

Это было полезно?

Решение

You can extend your breeze entities by using the metadataStore.registerEntityTypeCtor method

In some of my projects I have a separate function to extend each entity.

So you will end up with something like this.

function extendProduct(metadataStore) {
    var ctor = function () {

    };
    var initialiser = function (entity) {
        entity.isSelected= ko.observable(false);

        return entity;
    };

    metadataStore.registerEntityTypeCtor('Product', ctor, initialiser);
}

I generally use the initialiser as it gets called after breeze has hydrated the entity by adding all its additional properties etc.

I generally stay away from the contstructor method as it gets called as the very first step in breeze hydrating the entity.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top