0
votes

I have an 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

UPDATE

I try adding the error on console log and follow your example and i have the same error is not a function http://screencast.com/t/bQTyV8XGD0Pk

 doctor.FullName = ko.computed(function () {
        var fullName = "";
        fullName += doctor.FirstName();
        if (doctor.FirstName()) {
            fullName += ' ' + doctor.FirstName();
        }
        fullName += ' ' + doctor.LastName();
        if (doctor.SurName()) {
             fullName += ' ' + doctor.SurName();
        }
        return fullName;
    });

var query = breeze.EntityQuery.from("Doctors").orderBy("Id")
doctorManager.executeQuery(query)
.then(function (data) {                    
    self.doctors.removeAll();
    self.doctors(data.results);
 })
 .fail(function(error) {
     console.log(error);
 });

I hope someone can help me

1
this is the response screencast.com/t/2pYhuqO3C at least FirsName and LastName have values - smashraid

1 Answers

0
votes

The error you are seeing in the screenshot is because your query is throwing an error that you are not handling. Attach a .fail(failFunction) on the end of your entityQuery.

You can't call doctor.Surname() if there is no Surname function that is attached. Calling doctor.Surname just returns a function that doesn't give you a value.

Odds are, you don't 100% get why it's not working because you don't understand how Knockout works. You probably don't yet understand the meaning of what I am describing above either. You need to understand how Knockout works first, then try to learn Breeze.

If you want to just make it work without understand how or why put this in there and continue on. This assumes that there is a property returned called MiddleName and SurName that are just empty.

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