如何设置骨干获取回调方法 [英] how to set backbone fetch callback method
问题描述
你如何设置backbonejs的获取方法jsonpCallback功能的名称?要添加的问题是我还用requireJS,所以我尽量不有一个全球性的功能,并按照AMD的模式。
How do you set the jsonpCallback function name for the fetch method of backbonejs? To add to the problem is I also using requireJS so i am trying not to have a global function and follow the AMD pattern.
我不能使用jQuery的从自动生成的方法名称的原因是,我想使用的有用于缓存的原因回调方法静态名称的API的开发者。
The reason I can't use the auto generated method name from jquery is the developer of the api I am using want's to have a static name of for the callback method for caching reasons.
样品code
define([
'jquery',
'underscore',
'backbone',
'marionette',
'paginator',
'models/item'], function($, _, Backbone, Marionette, Paginator, modelItem) {
'use strict';
var PaginatedCollection = Paginator.requestPager.extend({
model: modelItem,
paginator_core: {
jsonpCallback : 'callbackFunc',
type: 'GET',
cache: true,
dataType: 'jsonp',
},
callbackFunc : function(data) {
console.log(data);
}
});
return PaginatedCollection;
});
错误消息
类型错误:callbackFunc不是一个函数
TypeError: callbackFunc is not a function
推荐答案
由于要缓存的结果(在浏览器中,也许在服务器上为好),你还需要设置选项。缓存
参数。
Since you want to cache the results (in the browser, and maybe on the server as well) you also need to set options.cache
param.
另外,参数是 jsonpCallback
不是 JSONP
下面就是你的同步过程的功能应该是这样的:
Here's what your sync over-ride function should look like:
sync: function (method, model, options) {
options.timeout = 10000; // required, or the application won't pick up on 404 responses
options.dataType = 'jsonp';
options.jsonpCallback = 'callbackFunc';
options.cache = 'true';
return Backbone.sync(method, model, options);
}
注:BackboneJS 1.0,在0.9或更早版本没有测试
NB: BackboneJS v1.0, not tested in 0.9 or earlier
这篇关于如何设置骨干获取回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!