1
votes

I'm having trouble creating computed values in Breeze and binding to them with Knockout.

I'm working with the NoDb sample project, which can be download here and Extending Entities article in the Breeze documentation has been my main source of information.

In the todo.datacontext.js file, I have made the following changes: (I've included the first and last lines to give guidance as to where exactly I've placed the code)

...
configureManagerToSaveModifiedItemImmediately();

metadataStore.registerEntityTypeCtor('TodoItem', function () { }, todoItemInitializer);

function todoItemInitializer(todoItem) {
    todoItem.participantName = ko.computed(function () {
        return 'John' + ' ' + 'Smith';
    });
};

var datacontext = {
...

And in Index.html, I added:

...
<ul data-bind="foreach: todos">
   <li>
       <p data-bind="text: participantName" />
       <input type="checkbox" data-bind="checked: isDone" />
...

However, I'm getting this error:

Error retrieving todo lists: Unable to parse bindings. Message: ReferenceError: participantName is not defined; Bindings value: text: participantName

I've also noticed, if I execute this:

metadataStore.getEntityType('TodoItem');

It's telling me that such a type doesn't exist, even though I'm registering it.

Any ideas?

1

1 Answers

0
votes

Digging deeper in to the documentation and the sample project, I've figured out that if the server is NOT providing any metadata than it must be manually created and registered in the client. See todo.model.js. Below is the most relevant part of the code:

var et = new EntityType({
            shortName: "TodoItem",
            namespace: "NoDb.Models",
            autoGeneratedKeyType: AutoGeneratedKeyType.Identity
        });

...

store.addEntityType(et);
store.registerEntityTypeCtor("TodoItem", null, todoItemInitializer);

Then the initializer works as expected and ko.computed values are recognized properly. Of course this puts you down a path of having to declare every single property you're going to use. Regular operations and bindings work without any of this code present.