2011-09-07 59 views
1

我正在使用Request.JSONP来获取一些数据,但我需要更改检索de数据的url中的回调函数。如何更改mootools类中的回调函数Request.JSONP

使用文档中的例子:

如何改变这种调用:

http://www.flickr.com/?format=json&jsoncallback=Request.JSONP.request_map.request_0

这样:

http://www.flickr.com/?format=json&jsoncallback=myOwnFunction

在jQuery中您可以使用设置“ jsonpCallback“来使用你自己的函数,但我找不到在MooTools中改变它的方法。

任何帮助,想法,将不胜感激。

编辑:

我的问题是,我的功能将是一个setInterval函数,每它执行请求的URL变化这样的时刻:

回调= Request.JSONP.request_map。 request_0

回调= Request.JSONP.request_map.request_1

回调= Request.JSONP.request_map.request_2 ... N

我需要最后一个数字不会改变(用于缓存目的)

回答

1

它被称为callbackKey JSONP。 read the doc

这里的扩展JSONP,我写(改变API PLS)在flickr类:

// the class 
Request.flickr = new Class({ 
    Extends: Request.JSONP, 
    options: { 
     callbackKey: "jsoncallback", 
     url: "http://www.flickr.com/services/rest/?", 
     log: true 
    }, 
    initialize: function(params, options) { 
     this.parent(options); 
     this.options.url = this.options.url + Object.toQueryString(params); 
    }, 
    success: function(data, script) { 
     this.parent(data, script); 
    }, 
    imageURL: function(obj) { 
     return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj); 
    } 
}); 

// example on how to use 
new Request.flickr({ 
    format: 'json', 
    api_key: "e7df6c74d2545f55414423463bf99723", // your api here 
    per_page: 4, 
    tags: "mountains", 
    method: "flickr.photos.search" 
}, { 
    onSuccess: function(data) { 
     target = document.id("action"); 
     var self = this; 
     data.photos.photo.each(function(el) { 
      new Asset.image(self.imageURL(el), { 
       onload: function() { 
        this.inject(target); 
       } 
      }); 
     }); 
    } 
}).send(); 

完全地覆盖这些回调,并通过在外部功能(需要是全球性的):

window.foo = function(data) { 
    console.log(data); 
}; 


Request.flickr = new Class({ 
    Extends: Request.JSONP, 
    options: {  
     url: "http://www.flickr.com/services/rest/?jsoncallback=foo&", 
     log: true 
    }, 
    initialize: function(params, options) { 
     this.parent(options); 
     this.options.url = this.options.url + Object.toQueryString(params); 
    }, 
    success: function(data, script) { 
     this.parent(data, script); 
    }, 
    imageURL: function(obj) { 
     return "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg".substitute(obj); 
    } 
}); 

// example on how to use 
new Request.flickr({ 
    format: 'json', 
    api_key: "e7df6c74d2545f55414423463bf99723", 
    // your api here 
    per_page: 4, 
    tags: "mountains", 
    method: "flickr.photos.search" 
}).send(); 

上面的代码将运行函数foo,将JSON对象传递给它。 http://jsfiddle.net/dimitar/DUtff/

+0

感谢您的帮助,我检查你的代码,我看到请求的URL是: HTTP://www.flickr.../ jsoncallback =富&FORMAT = JSON&API_KEY = * =方法flickr.photos .search&callback = Request.JSONP.request_map.request_0 有什么方法可以删除或更改最后一部分? (callback = Request.JSONP.request_map.request_0) 我的问题是,我的函数将在setInterval函数中,并且每次它执行所请求的url更改,如下所示: callback = Request.JSONP.request_map.request_0 callback = Request.JSONP.request_map.request_1 ... n 我不需要更改最后一位 –

+0

然后不要使用Request.JSONP,而是通过Asset.javascript或其他方式编写自己的包装。 Request.JSONP将始终输出它。尽管如此,你可以重构它并让它不能这样做。 –

+0

好的,对于这些建议 –