wes
wes

Reputation: 168

Setting Breeze Navigation Property in Knockout subscription

I am building a SPA (Single Page Application) using Breezejs and Knockoutjs.

I am running into an issue when trying to set a navigation property inside a knockout subscription. On the final line of the ko.subscription the console.log function shows me the entity, however, the WebPresences navigation property is null.

Not sure if the fact its in a ko.subscription really matters but I've been able to set the navigation prop just in a js function I call right before save, so I think it has some significance.

So here is my Entity Model

public partial class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Nullable<int> WebId { get; set; }

    public virtual WebPresence WebPresence { get; set; }
}

And here is my ko.subscription and relevant variables:

var vm = {
    newEntity: ko.observable(datacontext.createBreezeEntity('Entity')),
    newWebPresence: ko.observable(datacontext.newBreezeEntity('WebPresence')),
}

vm.newEntity().WebPresence.subscribe(
    function (data) {
        var self = this;
        if (data === null)
            self.target(vm.newWebPresence());
        console.log(vm.newEntity());
    }
);

And last but not least my datacontext

createBreezeEntity: function (entityName) {
    return manager.createEntity(entityName);
},

newBreezeEntity: function (entityName) {
    return manager.metadataStore.getEntityType(entityName).createEntity();
}

Upvotes: 0

Views: 128

Answers (1)

Ward
Ward

Reputation: 17863

I do not understand what you're striving to accomplish.

One thing I'm pretty confident about ... is that your the datacontext.newBreezeEntity creates an object that is just hanging in thin air and isn't part of any navigation property.

Let's look at your datacontext.newBreezeEntity method:

return manager.metadataStore.getEntityType(entityName).createEntity();

This indeed does create a new entity of the type named entityName. But this is a 'proto-entity'. It does not belong to an EntityManager ... certainly not to the manager instance. It isn't related to any particular other entity (e.g., your mysteriously-named Entity entity). It's just a detached entity.

I'm betting you thought that it would belong to manager because you started the expression there. Well it doesn't. You lost the connection with manager the moment you asked for its metadataStore.

But there is so much else that makes no sense to me at all. I can't tell why you're subscribing to vm.newBreezeEntity nor why it's called "newBreezeEntity" nor what it's relationship is supposed to be to vm.newEntity nor what you think this is within the subscription function.

Perhaps you should step back and describe what you WANT this code to do.

Upvotes: 1

Related Questions