【发布时间】:2017-06-30 13:41:53
【问题描述】:
我成功地创建了一个帮助器和函数,它使用把手在样式列表中显示来自 json 文件的数据。但是我正在努力查看我的代码的更多部分。基本上我想要实现的是,如果用户单击列表item,handlebars 应该获取该项目的 id 并将其显示在不同的页面上。我的控制台正确显示在新页面(查看详细页面)上单击的每个项目的正确 id,但该页面仅显示样式但没有来自我的内容json.这是我的车把代码:
function getParameterByName(name,url){
'use strict';
if(!url)url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]"+name+"(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if(!results)return null;
if(!results[2])return '';
return decodeURIComponent(results[2].replace(/\+/g,""));
}
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'jj.json');
ourRequest.onload = function () {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
createHTML(data);
} else {
console.log("You are connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
function createHTML(myData) {
var rawTemplate = document.getElementById("myTemplate").innerHTML;
var compiledTemplate = Handlebars.compile(rawTemplate);
var myContainer = document.getElementById("this-container");
var ourGeneratedHTML = compiledTemplate(myData);
myContainer.innerHTML = ourGeneratedHTML;
var characterId = getParameterByName("id");
console.log(characterId);
$.ajax("jj.json").done(function(cast){
if ($("body").hasClass("viewdetail")){
///i cant seem to figure out how to render it here
var ourGeneratedHTML = compiledTemplate(myData);
myContainer.innerHTML = ourGeneratedHTML;
} else
var ourGeneratedHTML = compiledTemplate(myData);
myContainer.innerHTML = ourGeneratedHTML;
{
}
});
}
而我的 html 如下:(viewdetails 页面)
<body class="viewdetail"
<div class="page-wrap">
<div id="this-container"></div>
</div>
<script id="myTemplate" type="text/x-handlebars-template">
<span class="username"><a href="#">{{cname}}</a></span>
</script>
我尝试过使用 {{#each}} 和 {{#each myjsonobject}} 但我一直在详细信息页面上获得正确的 ID,但样式中没有数据。 我在这里做错了什么,请帮忙:( 我的详细信息页面正在从我的索引页面中引用如下:
<a href="detailpage.php?id={{id}}" class="btn btn-primary btn-xs view-detail"><b>View</b></a>
这是我的 json 数据的一部分:
{"content":[{"id":"60","cname":"Admin","crep":"wew","sub_type":"memember ","token":"887618243","reference":"#5754","company_email":"rereee@gmail.com","company_phone":"234556566","service1":"erty","service2":"vbnmj","service3":"yjyt","service4":"uikjyj","company_website":"www.memeber.com","company_address":null,"company_location":"namibia","company_shortbio":"sasasa","company_longbio":"bvbvbvbvb","logos":"891319.png","logopath":"","imageA":"309129.png","imageB":"898798.jpg","imageC":"452926.jpg","imageD":"326704.jpg","company_facebook":null,"company_twitter":null,"company_linkedin":null,"company_stumbleupon":null,"company_pinterest":null,"company_googleplus":null,"user_backgroundpicture":null,"created_on":"2017-01-20 15:37:02","company_skype":null,"company_youtube":null,"company_vimeo":null,"headerimg":"993621.png","join_date":"2017-01-20 15:37:02.069147":""}
【问题讨论】:
-
对象
myData是什么样的? -
console.log(myData);在控制台上显示我的 json 文件中的对象数组
-
所以我修复了这个问题,控制台上的 myData 对象显示了我的 json 文件中的对象数组,当我在控制台记录 characterId 时,我得到每个视图的正确 id。但仍然没有数据,只是在查看详细信息页面上设置样式