最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fluent navigation with knockout.js - Stack Overflow

programmeradmin0浏览0评论

I have one single-page application with three sub-pages.

Url structure is:

/page  (sub-page-0)

/page#sub-page-1

/page#sub-page-2

View Model is:

function ViewModel(activePage){
   var self=this;

   self.activePage=ko.observable(activePage);
   self.changePage=function (newPage){
       self.activePage(newPage);
       //change url
   });
}

When user enter url I want to get # part in javascript and construct ViewModel with this parameter.

When user click link in the page I want to change URL and load that sub page.

I have one single-page application with three sub-pages.

Url structure is:

/page  (sub-page-0)

/page#sub-page-1

/page#sub-page-2

View Model is:

function ViewModel(activePage){
   var self=this;

   self.activePage=ko.observable(activePage);
   self.changePage=function (newPage){
       self.activePage(newPage);
       //change url
   });
}

When user enter url I want to get # part in javascript and construct ViewModel with this parameter.

When user click link in the page I want to change URL and load that sub page.

Share Improve this question asked Aug 9, 2012 at 8:34 Oguz KaradenizliOguz Karadenizli 3,5057 gold badges43 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You could use one of the solutions from the stack overflow question: How can I get query string values in JavaScript?

However, I strongly remend you consider a client side routing library. The knockout homepage has an example using sammyjs, but there are many other interesting solutions out there (including knockback, which bines backbonejs and knockoutjs).

Knockout itself does not support navigation. But you can use third party frameworks. There is a tutorial named "Single page applications" from knockout at http://learn.knockoutjs./ which shows how to do that with sammy.js.

发布评论

评论列表(0)

  1. 暂无评论