Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with observablearrays arraychange events i get deleted/added dublicates with child observables #18

Closed
pksorensen opened this issue Apr 27, 2014 · 2 comments

Comments

@pksorensen
Copy link

I have chained some arrays like this:

    private flights: KnockoutObservableArray<Flight> = ko.observableArray([]);
    private selectedFlights = this.flights.filter((dataset) => dataset.isSelected());
    private selectedFlightsViewModels = this.selectedFlights.map((f) => new DataSetItemViewModel(f, this));

if using arraychange event on selectedFlightViewModels it seem to firing a added/deleted event of the same item when a property observable in the DataSetItemViewModel is changed.

Is this intended?
no array changed events fired on selectedFlights so its only happening when I change something on the DataSetItemViewModel and I am trying to get around that because it flushes all the UI bindings

@SteveSanderson
Copy link
Owner

Yes, if your mapping function evaluates an observable, then the only way Knockout-Projections can update the mapping output is to remove the old item and add a newer version of it with the updated observable value.

If you don't want that to happen, then make sure the observable dependency is scoped to the DataSetItemViewModel object itself. For example, don't write this:

function DataSetItemViewModel(flight) {
    this.price = flight.price();
}

Instead, write either this:

function DataSetItemViewModel(flight) {
    this.price = flight.price;
}

Or this, if you need to apply some transformation to the incoming data:

function DataSetItemViewModel(flight) {
    this.price = ko.computed(function() {
        return flight.price() * 100;
    });
}

With either of these latter two code blocks, the subscription on the price observable is not an aspect of the mapping - it's scoped within the DataSetItemViewModel.

Hope that makes sense!

@pksorensen
Copy link
Author

Makes sende. Thank you for explaining. And off topic - thumbs up for using ko in the new azure portal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants