HTML5,淘汰url重定向功能
HTML5, Knockout url redirect function
我创建了一个 HTML5 应用程序,它使用 knockoutjs 调用 restful 服务和 get/post JSON 消息。
这是我使用 HTML5 的第一个应用程序,所以我不确定如何实现 URL 重定向。
在应用程序中,我有两个 html 页面,一个是 DataGrid 页面,它显示通过执行 get rest 调用接收到的所有数据。我在显示中的一个字段中添加了一个超链接,我想用它重定向到详细信息页面并进行其余调用以获取该特定 id 的数据并稍后在可编辑页面中显示它想保存更改.
UserGridPage.html
<tbody data-bind="foreach:userList">
<tr>
<td><a data-bind="attr:{href: userInfoUrl()}, text:userInfoId"></a></td>
<td data-bind="text: UserName" ></td>
</tr>
UserGridPage.js
MyUserInfoViewModel = function () {
var self = this;
self.userList = ko.observableArray();
$.getJSON("http://localhost:8080/user/webresources/userinfo/").
then(function (userinfos) {
$.each(userinfos, function () {
self.userList.push({
userInfoUrl:ko.observable('/USERUI/UserEntry.html#'+this.userInfoId),
userInfoId:ko.observable(this.userInfoId),
policyHolderEmail: ko.observable(this.policyHolderEmail),
});
});
});
我想知道 UserEntry 页面如何知道哪个 ID 被传递到它的页面,以及我如何进行其余调用以将 Id 传递给 restful URL .
感谢任何有关代码示例、链接等的帮助。
谢谢
不确定如何使用 javascript 从 URL 获取参数,但如果您愿意使用 PHP,则可以使用 $_REQUEST['Id'] 使用它来生成新的 REST 调用。
因此您的 href 将类似于“/USERUI/UserEntry.php?Id=5”
要在您的 UserEntry 页面上的 javascript 中使用该变量,您可以执行以下操作:
<script>
var id = <?php $_REQUEST['Id']; ?>
//generate your restful query here.
</script>
你应该这样尝试
查看:
<ul data-bind="foreach: items">
<li>
<a data-bind="attr: { href: id }, text: name,click:function(){alert(id)}"></a>
</li>
</ul>
视图模型:
var viewModel = {
items: [{'id':1,'name':"one"},{'id':2,'name':"two"},{'id':3,'name':"three"}]
};
ko.applyBindings(viewModel);
样本工作 fiddle here
在你的userEntry.html中,如果你想获取传递的id值:
<script>
var id = window.location.hash;
</script>
我记得 firefox 的行为略有不同,它会解码片段中的 URL 编码字符,所以如果你想要原始内容,也可以考虑使用:
<script>
var id = window.location.href.split("#")[1];
</script>
我创建了一个 HTML5 应用程序,它使用 knockoutjs 调用 restful 服务和 get/post JSON 消息。 这是我使用 HTML5 的第一个应用程序,所以我不确定如何实现 URL 重定向。
在应用程序中,我有两个 html 页面,一个是 DataGrid 页面,它显示通过执行 get rest 调用接收到的所有数据。我在显示中的一个字段中添加了一个超链接,我想用它重定向到详细信息页面并进行其余调用以获取该特定 id 的数据并稍后在可编辑页面中显示它想保存更改.
UserGridPage.html
<tbody data-bind="foreach:userList">
<tr>
<td><a data-bind="attr:{href: userInfoUrl()}, text:userInfoId"></a></td>
<td data-bind="text: UserName" ></td>
</tr>
UserGridPage.js
MyUserInfoViewModel = function () {
var self = this;
self.userList = ko.observableArray();
$.getJSON("http://localhost:8080/user/webresources/userinfo/").
then(function (userinfos) {
$.each(userinfos, function () {
self.userList.push({
userInfoUrl:ko.observable('/USERUI/UserEntry.html#'+this.userInfoId),
userInfoId:ko.observable(this.userInfoId),
policyHolderEmail: ko.observable(this.policyHolderEmail),
});
});
});
我想知道 UserEntry 页面如何知道哪个 ID 被传递到它的页面,以及我如何进行其余调用以将 Id 传递给 restful URL .
感谢任何有关代码示例、链接等的帮助。
谢谢
不确定如何使用 javascript 从 URL 获取参数,但如果您愿意使用 PHP,则可以使用 $_REQUEST['Id'] 使用它来生成新的 REST 调用。
因此您的 href 将类似于“/USERUI/UserEntry.php?Id=5”
要在您的 UserEntry 页面上的 javascript 中使用该变量,您可以执行以下操作:
<script>
var id = <?php $_REQUEST['Id']; ?>
//generate your restful query here.
</script>
你应该这样尝试
查看:
<ul data-bind="foreach: items">
<li>
<a data-bind="attr: { href: id }, text: name,click:function(){alert(id)}"></a>
</li>
</ul>
视图模型:
var viewModel = {
items: [{'id':1,'name':"one"},{'id':2,'name':"two"},{'id':3,'name':"three"}]
};
ko.applyBindings(viewModel);
样本工作 fiddle here
在你的userEntry.html中,如果你想获取传递的id值:
<script>
var id = window.location.hash;
</script>
我记得 firefox 的行为略有不同,它会解码片段中的 URL 编码字符,所以如果你想要原始内容,也可以考虑使用:
<script>
var id = window.location.href.split("#")[1];
</script>