好的,我已经放弃尝试破解它,请帮助社区:
我正在尝试映射来自服务器的 JSONP 响应以填充此 HTML:
<!-- ko foreach: featConfirm.confirmPages -->
<div data-role="page" class="featConfirm" data-bind="template: {name: 'featConfirm_tmp'}, attr: {'id': featConfirm.assignPageID($data.position), 'pos': $data.position}" ></div>
<!-- /ko -->
<script type="text/html" id="featConfirm_tmp">
<div data-role="content">
<div class="header"><img class="owner-image" src="img/filler.jpg" />
Did <span class="owner-name" data-bind="text: featConfirm.featOwner.full_name"></span>
</div>
</div>
</script>
这是 js 设置 - 这是迄今为止我最好的猜测,但显然它不起作用,但它确实创建了正确数量的带有 id 的页面,但是,我无法从 JSON 数组中的其他位置访问任何数据:
function master(){
var self = this;
self.featConfirm = new function(){
var self = this;
/* KO observable used to populate view */
self.confirmPages = ko.observableArray([]);
/* AJAX call to server to get confirmable feats */
self.getFeatsForConfirm = function(){
jQuery.ajax({
url: sourcesURL + 'myPHP.php',
type: 'POST',
dataType: 'jsonp',
jsonp: 'jsoncallback',
success: function(bData, textStatus, jqXHR){
/* this is my best guess so far but obviously it does not work however it does create the proper number of pages with id's I can't access any data from elsewhere in the JSON array */
for (i=0;i<bData.length;i++){
var a = {position: i+1, data: ko.mapping.fromJSON(bData[i])};
self.confirmPages.push(a);
}
},
error: function(jqXHR, textStatus, errorThrown){
console.log('There was an error submitting your request');
}
});
};
}
}
/* init Knockout */
ko.applyBindings(master());
我的 JSON 是这样的:
([
{
"featDetails":{"attempt":"39","owner":"2"},
"featSpecs":{"id":"1347387875","name":"Eat a tomato"},
"featOwner":{"full_name":"Darth Freakin Vader"}
},
{
"featDetails":{"attempt":"44","owner":"1"},
"featSpecs":{"id":"1352986771","name":"Drink Coffee"},
"featOwner":{"full_name":"Luke Sywalker"}
}
])
我显然从根本上误解了映射插件的工作原理,这就是我求助于你的原因。我已经从JSON数据的每个数组中删除了很多值,这就是我想使用插件的原因,但这是基本结构。
- 是不是可以通过这种方式用映射插件映射JSONP,
- 我没有得到什么,
- 我将如何正确映射这些数据,以便我可以在 HTML 中访问它。
在此先感谢您的帮助...