从JavaScript对象访问数据's数组成员变量
Accessing Data from JavaScript Object's Array Member Variable
我正在编写一个jQuery插件,用于使用Google的feed API获取RSS提要数据。使用这个API,我将所有相关的RSS提要数据保存到一个对象中,然后通过方法对其进行操作。我有一个功能,应该是将RSS提要呈现到网页上。不幸的是,当我尝试显示单独的RSS提要条目时,我遇到了一个错误。这是我的相关代码:
var RSSFeed = function(feedTitle, feedUrl, options) {
/*
* An object to encapsulate a Google Feed API request.
*/
// Variables
this.description = "";
this.entries = [];
this.feedUrl = feedUrl;
this.link = "";
this.title = feedTitle;
this.options = $.extend({
ssl : true,
limit : 4,
key : null,
feedTemplate : '<article class="rss-feed"><h2>{title}</h1><ul>{entries}</ul></article>',
entryTemplate : '<li><h3><a href="{link}">{title}</a></h3><p>by: {author} @ {publishedDate}</p><p>{contentSnippet}</p></li>',
outputMode : "json"
}, options || {});
this.sendFeedRequest = function() {
/*
* Makes the AJAX call to the provided requestUrl
*/
var self = this;
$.getJSON(this.encodeRequest(), function(data) {
// Save the data in a temporary object
var responseDataFeed = data.responseData.feed;
// Now load the data into the RSSFeed object
self.description = responseDataFeed.description;
self.link = responseDataFeed.link;
self.entries = responseDataFeed.entries;
});
};
this.display = function(jQuerySelector) {
/*
* Displays the RSSFeed onto the webpage
* Each RSSEntry will be displayed wrapped in the RSSFeed's template HTML
* The template markup can be specified in the options
*/
var self = this;
console.log(self);
console.log(self.entries);
};
};
$.rssObj = function(newTitle, newUrl, options) {
return new RSSFeed(newTitle, newUrl, options);
};
// Code to call the jquery plugin, would normally be found in an index.html file
rss = $.rssObj("Gizmodo", "http://feeds.gawker.com/Gizmodo/full");
rss.sendFeedRequest();
rss.display($('div#feed'));
显然,我的display()
函数还不完整,但它是一个很好的例子。第一个console.log()
将把所有相关数据写入控制台,包括entries
数组。然而,当我尝试自己记录entries
数组时,它返回的是一个空数组。知道为什么吗?
我想问题是display()
在没有等待AJAX请求完成的情况下被调用。因此,当您已经尝试访问entries
时,请求仍在运行,因此为空数组。
为了解决这个问题,您可以将对display()
的调用移动到$.getJSON()
的回调中。您只需要添加所需的选择器作为参数:
this.sendFeedRequest = function(selector) {
var self = this;
$.getJSON(this.encodeRequest(), function(data) {
var responseDataFeed = data.responseData.feed;
...
self.entries = responseDataFeed.entries;
self.display(selector);
});
};
编辑:
如果您不想将display()
移动到回调中,您可以尝试以下操作(未经测试!):
var RSSFeed = function(feedTitle, feedUrl, options) {
...
this.loading = false;
this.selector = null;
this.sendFeedRequest = function() {
var self = this;
self.loading = true;
$.getJSON(this.encodeRequest(), function(data) {
...
self.loading = false;
if (self.selector != null) {
self.display(self.selector);
}
});
};
this.display = function(jQuerySelector) {
var self = this;
if (self.loading) {
self.selector = jQuerySelector;
}
else {
...
}
};
};
相关文章:
- javascript数字数组
- 按数字数组禁用天数jquery日期选择器
- 将数字数组转换为合适的正则表达式
- 敲除foreach数字数组
- 砖块和球之间的碰撞检测(使用数字数组)
- 筛选数字数组,其中 0 是有效输入
- 如何使用JavaScript/Jquery/Knockout JS将数字数组绑定到范围滑块
- javascript数组既可以是数字数组,也可以是关联数组
- 将数字转换为反转的数字数组
- Javascript反转数字数组,然后减去2,然后是1,然后是0,再加1,再加2等等
- 在JavaScript中添加多维数字数组的和
- 淘汰赛获胜;t处理多选列表的数字数组
- 如何使用数字数组作为表单输入名称
- 使用 meteor.js从 MonoDB 检索数字数组
- 为什么我不能将数组作为成员添加到我的对象中
- Javascript 全局数组访问成员未定义
- 使用Knockout映射插件在子数组的成员上定义计算可观察对象
- 如何将对象数组作为道具传递给组件,然后将数组的成员作为道具传递给嵌套组件?
- 数组的成员是未定义的,它拥有获取该成员所需的所有数据
- 如何将数组的成员定义为对象