If Binding Magic! Avoiding Duplication Problem in Header Element IDS

Uncategorized

Welcome again. As you may already have seen my post regarding creating Sinlge Page Application using knockoutjs and jquery mobile.
One thing i wanted to discuss here that you might encounter a simple problem of duplicating the id attribute in header elements.
Why that can happen? Because you are using a template for header and this template is being injected in each page/section. Of
course the ids of header elements will be duplicated and you wont be able to make jquery work using ids and won’t be able to get
the item selected you want on some particular page.
I have seen many questions from people asking how this can be solved. So i thought i should present a simple solution.

Problem:
To understand the problem, consider this example. Here is a template


This template is being called on each section/page.

Solution:

While working on SPA using this approach it is very simple to avoid this problem. You just need a property in parent view model. Let see how we can do this

function VM() {
	var self = this;

	self.Title = ko.observable('Knockout! Parent and Child Usage Techniques')
	self.Home  = ko.observable()
	self.Form  = ko.observable()
	self.List  = ko.observable()

	/*	Define a property to hold id of current page	*/
	self.CurrentPage = ko.observable()
	
	.
	.
	.
}	

And on page change event use this

$(window).on( "pagechange", function( event, data ) {
	var page_id	=	data.toPage[0].id
	/*	
		While navigating among pages each time pass 
		the page id to CurrentPage property	
	*/		
	vm.CurrentPage(page)
	
	switch(page_id)
	{
		case 'form':
		    vm.FormPage().LoadData()
			break;						
		case 'list':
			vm.ListPage().LoadData()	
			break;						
		default:
			vm.HomePage().LoadData()
			break;
	}
});	

And now on view you need a simple condition

// Other content
// Other content
// Other content

Now what this does is when you navigate between pages the other contents in each sections gets removed and the page being viewed is generated. So if you inspect section elements you will see only current page is having content and all other sections are empty. This way you can avoid duplication of id attributes in header. Hope this simple technique is useful.

You can view a demo here. Any question, i am here to explain.

If Binding Magic! Avoiding Duplication Problem in Header Element IDS

Uncategorized

Welcome again. As you may already have seen my post regarding creating Sinlge Page Application using knockoutjs and jquery mobile.
One thing i wanted to discuss here that you might encounter a simple problem of duplicating the id attribute in header elements.
Why that can happen? Because you are using a template for header and this template is being injected in each page/section. Of
course the ids of header elements will be duplicated and you wont be able to make jquery work using ids and won’t be able to get
the item selected you want on some particular page.
I have seen many questions from people asking how this can be solved. So i thought i should present a simple solution.

Problem:
To understand the problem, consider this example. Here is a template


This template is being called on each section/page.

Solution:

While working on SPA using this approach it is very simple to avoid this problem. You just need a property in parent view model. Let see how we can do this

function VM() {
	var self = this;

	self.Title = ko.observable('Knockout! Parent and Child Usage Techniques')
	self.Home  = ko.observable()
	self.Form  = ko.observable()
	self.List  = ko.observable()

	/*	Define a property to hold id of current page	*/
	self.CurrentPage = ko.observable()
	
	.
	.
	.
}	

And on page change event use this

$(window).on( "pagechange", function( event, data ) {
	var page_id	=	data.toPage[0].id
	/*	
		While navigating among pages each time pass 
		the page id to CurrentPage property	
	*/		
	vm.CurrentPage(page)
	
	switch(page_id)
	{
		case 'form':
		    vm.FormPage().LoadData()
			break;						
		case 'list':
			vm.ListPage().LoadData()	
			break;						
		default:
			vm.HomePage().LoadData()
			break;
	}
});	

And now on view you need a simple condition

// Other content
// Other content
// Other content

Now what this does is when you navigate between pages the other contents in each sections gets removed and the page being viewed is generated. So if you inspect section elements you will see only current page is having content and all other sections are empty. This way you can avoid duplication of id attributes in header. Hope this simple technique is useful.

You can view a demo here. Any question, i am here to explain.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *