Use knockout.js to prevent data from being loaded again on component change


Alvaro

I am using a component to switch between one view or another. That's what I recommend doing .

Now, every time you switch between views, the data is loaded again. This is a reproduction of the question. (check console).

In the example I provided, it's no big deal, but when calling external APIs, it's no big deal.

How can I avoid it?

As a related question, currently it's loading twice calling the API (or loading the data in my example). Once per registered component. Nor should it be.

Should I use jQuery/Javascript to call the API with Ajax and then use it after setting the data in the view model?

$.getJSON("/some/url", function(data) { 
    viewModel.setData(data); 
})
James Thorpe

The problem is that in the component definition you are using the constructor function method specified for the component viewModel, so every time you use the component it is actually instantiating a whole new viewmodel.

As a related question, currently it's loading twice calling the API (or loading the data in my example). Once per registered component. Nor should it be.

Note that it's called once because you're calling new viewModel()yourself, and once to instantiate the initial component that shows up when the extra viewmodel is instantiated. It wasn't logged twice because you registered two components - if you registered more components , you'd see that it was only logged twice.

A quick workaround is to specify the following using a shared object instance method :viewModel

var vm = new viewModel();

ko.components.register('summary', {
    viewModel: {instance: vm},
    template: '...'
});

ko.components.register('details', {
    viewModel: {instance: vm},
    template: '...'
});

ko.applyBindings(vm);

You have to decide if this is the right approach for your entire project in the long run, there are several ways to pass data into the component, this is just one of them.

Here is an updated fiddle , recorded only loading dataonce.

Related


How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile inside the specfolder I want to use with RSpec . However, this file is loaded every time

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

Knockout.js callback whenever a component is loaded into the DOM

Havardu I am writing a single page application using kickout.js. I want all textboxes in the application to behave in a certain way, for example, to select all the text currently in the set. To avoid duplicate solutions like adding custom bindings to all input

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

Knockout.js callback whenever a component is loaded into the DOM

Havardu I am writing a single page application using kickout.js. I want all textboxes in the application to behave in a certain way, for example, to select all the text currently in the set. To avoid duplicate solutions like adding custom bindings to all input

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist