gpt4 book ai didi

JavaScript 和 jQuery : accessing class member variables from event callback

转载 作者:行者123 更新时间:2023-11-28 09:08:37 24 4
gpt4 key购买 nike

我正在编写一个 Javascript SDK 来与 Web 服务交互。我正在使用 jQuery 进行 AJAX 调用。

当 AJAX 调用失败时,我已为 ajaxError 注册了一个事件处理程序,该事件处理程序在 .js 文件顶部调用。我的问题(我不明白为什么)是,当它被调用时,我无法访问我的 Akamanda.Client 的类成员变量。

我尝试为 Akamanda.Client 添加另一种方法作为 .prototype.logError,该方法由 jQuery Ajax 处理程序调用,但即使这样, (this.logging) 的测试也失败了。

如何从 jQuery 回调访问类成员变量?我在这里不明白什么? Akamanda.Client.logging 在 ajaxError 回调中未定义。

我的 SDK 代码:

$(document).ajaxError(function(event, jqxhr, settings, exception) {
// more robust error handling for different conditions
if (Akamanda.Client.logging) {
console.log('FAILED: ' + settings.type + ' ' + settings.url + ' => ' + exception);
}
});

Akamanda.Client = function(options) {

this.URL = options.URL || 'http://m-test.akamanda.com';
this.baseURL = this.URL + '/api/' + Akamanda.API_VERSION;
this.feedsURI = '/websyndication/feed/';

// who is the client? (iphone/android/web)
this.clientName = options.clientName;

// For development: Logging and buildcurl IS ON, for production: OFF
//this.logging = options.logging || true;
this.logging = true;

// called when a user is not authorised (Disabled)
// this.logoutCallback = options.logoutCallback || null;
}

Akamanda.Client.prototype.getFeeds = function(callback){
var feeds = [];
$.getJSON(this.baseURL + this.feedsURI, function(data) {
$.each(data, function(index, feed) {
feeds[index] = {
name: feed.name,
title: feed.title,
link: feed.link
};

})
callback(feeds);
});//.error(function(err) { (disabled at the moment in favour of ajaxError event)
// console.log('Error: ' + err.error);
// });
}

我的客户端代码(在另一个 JS 源文件中):

var options = { logging: true };
myAPI = new Akamanda.Client(options);
var feeds = [];
var articles = [];

function getFeeds()
{
myAPI.getFeeds(function(AkamandaFeeds) {
feeds = AkamandaFeeds;
showFeeds();
});
}

最佳答案

据我从您发布的代码中看到,您从未实例化 Akamanda.Client 类型的对象。

var Client = new Akamanda.Client();

var Akamanda.Client = {};

Akamanda.Client.logging = ....

JSBin 示例:http://jsbin.com/ajidig/1/edit

好的,这里有一个小例子(真实的代码,但非常简化):

//we wrap our code in a self invoking function so that we don't pollute the global    namespace, see http://stackoverflow.com/questions/6715805/self-invoking-functions-javascript for further details
(function(){
//create your object that holds all your function, that are different ways to do this
var Akamanda = {};

//a private function
function ErrorHandler(clientObj) {
this.clientObj = clientObj;
//do whatever with clientObj
this.log = function(){..}
}
//private constructor for clientobj
function Client(options){
..
}



Akamanda.Client = function(){

var newClient = new Client({..});
//setup
Akamanda.ErrorLogging = new ErrorHandler(newClient);
return newClient;
}

//bind our service to the window object to make it accesible

window.Akamanda = Akamanda;
})()


//client

var myAPI = Akamanda.Client();
Akamanda.ErrorLogging.log();

我希望这个基本示例有所帮助。如果你需要了解更多关于Javascript模式的知识,我可以推荐这本书http://jsninja.com/作者:John Resig,jQuery 的创建者。根据您想要做什么,还有很多框架,例如 http://backbonejs.org/这有助于处理这类事情。

关于JavaScript 和 jQuery : accessing class member variables from event callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com