2014-02-19 85 views
2

我正在使用jQuery Mobile和knockout.js的跨平臺移動應用程序。我需要知道重定向到下一頁的正確方法。我見過的所有例子都說明了一個包含多個頁面的HTML文件。是否可以爲每個頁面創建單獨的HTML文件併爲每個HTML頁面維護一個單獨的JS文件。另外,如何將視圖模型鏈接到每個單獨的頁面?頁面導航 - jQuery Mobile與knockout.js

我的問題如下...

  1. 我如何從的index.html重定向到Search.html和這些頁面之間傳遞數據?
  2. 如何爲每個頁面創建單獨的HTML並鏈接到視圖模型?

工作代碼示例

的index.html

<div data-role="page" id="Index"> 
     <div data-role="header"> 
      <h1> 
       jQM</h1> 
     </div> 
     <div data-role="content" data-iscroll> 
      This is jQM Testing 
      <label for="arrCity"> 
      </label> 
      <input type="text" class="autocomplete" data-bind="value:aCity" /> 
      <div data-bind="aCity"> 
      </div> 
      <input id="btn" type="button" data-bind="click:showData" value="showData" /> 
     </div> 
     <div data-role="footer"> 
      <h1> 
       jQM Footer</h1> 
     </div> 
    </div> 

search.html

div id="second" data-role="page"> 
    <div data-role="header"> 
     <h1 data-bind="text: greeting"> 
     </h1> 
    </div> 
</div> 

Index.js

$(document).ready(function() { 
    ko.applyBindings(ReqModel, document.getElementById('Index')); 

}); 



var ReqModel = new ReqModel(); 
function ReqModel() { 
    this.aCity = ko.observable(''); 
    this.showData = function() { 
     alert(this.aCity()); 
     $.mobile.changePage("search.html"); 


    }; 
} 

search.js

var ResviewModel = new ResviewModel(ReqModel); 


function ResviewModel(data) { 
    alert("this load"); 
} 

$(document).ready(function() { 
    var pre = document.getElementById('Index'); 
    ko.cleanNode(pre); 
    ko.applyBindings(ResviewModel, document.getElementById('second')); 
}); 

回答

0

關於你的第一個問題:

如何從的index.html重定向到Search.html並在這些頁面之間傳遞數據?

我解決與jQuery.navigate問題,(也可NuGet)我希望這可以幫助?

0

我建議你這樣做。 而不是使用輸入使用錨

<a href="search.html" id='btn' data-bind="click:showData">showData</a> 

確保函數調用的返回真或假

this.showData = function() { 
    alert(this.aCity()); 
    return true 
}; 

返回true,將讓你移動到下一個頁面。

對於結構方式你可以看到this