Javascript $.get JSON请求+;填充和返回数组
我开始使用移动框架LungoJS。Me和javascript的工作不是很好,但我确实希望修改以下原始代码: ORIGINAL.JSJavascript $.get JSON请求+;填充和返回数组,javascript,jquery,Javascript,Jquery,我开始使用移动框架LungoJS。Me和javascript的工作不是很好,但我确实希望修改以下原始代码: ORIGINAL.JS var mock = function() { var mock = []; for (var i=1; i<=5; i++){ mock.push({ id: i, name: 'name n'+i, des
var mock = function() {
var mock = [];
for (var i=1; i<=5; i++){
mock.push({
id: i,
name: 'name n'+i,
description: 'description n'+i
})
}
lng.View.Template.List.create({
container_id: 'lives',
template_id: 'show_music_template',
data: mock
})
}
return {
mock: mock
}
})(LUNGO, App);
var mock = function() {
var mock = [];
var url = 'http://localhost/app/rest/podcasts';
var data = {};
//lng.Service.get = $get
lng.Service.get(url, data,function(response) {
var array = [];
//Do something with response
jQuery.each(response.result, function() {
mock.push({
id: this.id,
name: this.name,
description: this.description
})
});
document.write(mock[1].id);
});
lng.View.Template.List.create({
container_id: 'lives',
template_id: 'show_music_template',
data: mock
})
}
return {
mock: mock
}
SERVICE.JS
var mock = function() {
var mock = [];
for (var i=1; i<=5; i++){
mock.push({
id: i,
name: 'name n'+i,
description: 'description n'+i
})
}
lng.View.Template.List.create({
container_id: 'lives',
template_id: 'show_music_template',
data: mock
})
}
return {
mock: mock
}
})(LUNGO, App);
var mock = function() {
var mock = [];
var url = 'http://localhost/app/rest/podcasts';
var data = {};
//lng.Service.get = $get
lng.Service.get(url, data,function(response) {
var array = [];
//Do something with response
jQuery.each(response.result, function() {
mock.push({
id: this.id,
name: this.name,
description: this.description
})
});
document.write(mock[1].id);
});
lng.View.Template.List.create({
container_id: 'lives',
template_id: 'show_music_template',
data: mock
})
}
return {
mock: mock
}
问题是外循环,我不能使用“模拟”数组。当然我犯了几个错误…但有人知道问题出在哪里吗
谢谢。问题是
$.get()
需要时间执行,因此是异步的。像这样的异步调用涉及使用回调
函数。要访问mock
数组,需要在此回调中嵌套任何内容
您还可以强制AJAX调用在jQuery中同步(尽管我和文档对此提出了警告);根据:
默认情况下,所有请求都是异步发送的(即设置为
默认情况下为true)。如果需要同步请求,请将此选项设置为
错。跨域请求和数据类型:“jsonp”请求不会
支持同步操作。请注意,同步请求可能会
暂时锁定浏览器,禁用请求时的任何操作
它是活动的
谢谢!!正如你所说,我用回调解决了这个问题 如果有人感兴趣,我会发布代码:
App.Services = (function(lng, app, undefined) {
var mock = function() {
var mock = new Array();
var url = 'http://localhost/app/rest/podcasts';
var data = {};
function getData (url,data,mock,callbackFnk){
lng.Service.get(url, data,function(response) {
//Do something with response
// now we are calling our own callback function
if(typeof callbackFnk == 'function'){
callbackFnk.call(this, response);
}else{
document.write("Error");
}
});
}
getData(url,data,mock,function(response){
jQuery.each(response.result, function() {
mock.push({
id: this.id,
name: this.name,
description: this.description
})
});
lng.View.Template.List.create({
container_id: 'lives',
template_id: 'show_music_template',
data: mock
})
})
}
return {
mock: mock
}
})(LUNGO, App);
删除
var
likemock=[]
声明一个全局数组,你会发现使用globals是一种反模式,应该避免。“要访问模拟数组,你需要在此回调中嵌套任何内容。”我是如何得到它的?谢谢你的帮助answer@galix85你知道什么是嵌套和回调函数吗?如果没有,我推荐一些基本的JS阅读。