【发布时间】:2013-02-09 06:52:30
【问题描述】:
好的,我已经放弃尝试破解它了,请帮助社区:
我正在尝试映射来自服务器的 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 数据的每个数组中删除了 ALOT 值,这就是我想使用该插件的原因,但这是基本结构。
- 这种方式可以用映射插件映射JSONP吗,
- 我在这里没有得到什么,
- 如何正确映射这些数据,以便可以在 HTML 中访问它。
提前感谢您的帮助...
【问题讨论】:
标签: ajax jquery-mobile knockout.js jsonp knockout-mapping-plugin