I have an entity from Breeze that might have 20 or so properties, but then I want to wrap the entity in a viewmodel and expose only those few properties as ko.observable(). How do I manage all to do the property change notification?
In c#, I would do something like this:
public double OtherSalesPercent
{
get { return Model.OtherSalesPercent; }
set
{
if (Model.OtherSalesPercent != value)
{
Model.OtherSalesPercent = value;
OnPropertyChanged("OtherSalesPercent");
}
}
}
UPDATE Jay thanks for the response. I understand that beeze does this. However, I don't want my view binding directly to the entities, but rather a viewmodel. So think of a view called Apples. And apples is showing a list of apple viewmodels. And each apple entity has 30 different properties, but I want to only expose 3 of those 30 properties on the apple viewmodel. So, I want to create 3 ko.observable properties on the apple viewmodel that will surface those 3 properties on the apple entity. What I have so far is "working", but I don't think its the cleanest. Here is the name property:
var appleViewModel = function(appleEntity) {
var backingEntity = appleEntity;
var name = ko.observable(locationEntity.name());
name.subscribe(function(newValue) {
backingEntity.name(newValue);
});
var vm = {
name: name
};
return vm
};
The other big reason, is I'm using jqxGrid, and they have issues binding to Breeze entities. So really what I'm wanting is a appleViewModel that will surface those properties, but however, if I change the actual apple entity (as in cancelChanges), I want the UI to get that change from the entity).
UPDATE 2
This seems to be giving me everything I want and binds to jqxGrid
var createViewModel = function(appleEntity) {
var entity = appleEntity;
var name = ko.observable(entity.name());
name.subscribe(function(newValue) {
entity.name(newValue);
});
entity.name.subscribe(function(newValue) {
name(entity.name());
});
var vm = {
name: name
};
return vm;
};
But this just seems so wrong to me. I'm basically wanting two-way binding. So when I change the viewmodel name property it changes the backing entity name... and if I change the backing entity name, it will update the viewmodels name property.
Thanks!