Javascript和jQuery:从事件回调中访问类成员变量

时间:2013-05-16 06:32:46

标签: javascript jquery

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

当AJAX调用失败时,我已经为我的.js文件顶部调用的ajaxError注册了一个事件处理程序。我的问题,我不明白为什么,当它被调用时,我无法访问我的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();
    });
}

1 个答案:

答案 0 :(得分:0)

从我发布的代码中我可以看到,你永远不会实例化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模式的更多信息,我可以推荐jQuery创建者John Resig撰写的这本书http://jsninja.com/。 根据你想要做的事情,还有很多像http://backbonejs.org/这样的框架可以帮助解决这类问题。