Javascript 主干网意外地发出多个ajax请求
我将fetchurl与deferred方法放在一起,我希望它只调用远程ajax请求一次 但是,当我加载页面时,它会调用三次 我怎样才能修好它?谢谢 js脚本 compeltejs脚本Javascript 主干网意外地发出多个ajax请求,javascript,ajax,backbone.js,Javascript,Ajax,Backbone.js,我将fetchurl与deferred方法放在一起,我希望它只调用远程ajax请求一次 但是,当我加载页面时,它会调用三次 我怎样才能修好它?谢谢 js脚本 compeltejs脚本 问题是您的注释集合在初始化时会触发fetch。它的方法wellFormedComments和emptyComments创建新的注释集合,以便它们也触发fetch 您可以通过在需要时手动触发fetch来解决此问题,例如: var Comments=Backbone.Collection.extend({ 模式:评论,
问题是您的注释集合在初始化时会触发
fetch
。它的方法wellFormedComments
和emptyComments
创建新的注释集合,以便它们也触发fetch
您可以通过在需要时手动触发fetch来解决此问题,例如:
var Comments=Backbone.Collection.extend({
模式:评论,
url:fetch_comments_url,
wellFormedComments:function(){
var消息限制长度=80
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
如果(msg!=null){
msg=msg.replace(/^\s+|\s+$/g'))
如果(msg.length>=消息\u限制\u长度){
model.set(“预览消息”,消息substr(0,消息限制长度/2));
}else{};
返回真值
}否则{
返回错误
};
});
返回新的评论(模型);
},
emptyComments:function(){
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
返回false==(msg).notBlank();
});
返回新的评论(模型);
}
});
var CommentView=Backbone.View.extend({
el:$(“#评论#部分”),
render:function(){
var notNullComments=comments.wellFormedComments();
如果(notNullComments.length>0){
$(“#dadasay_comments_plugin”).show();
}
var html=commentsTmpl(notNullComments.toJSON());
$(this.el).append(html);
},
});
var EmptyCommentView=Backbone.View.extend({
el:$(“空注释列表”),
render:function(){
var source=$('#空的#comments_list_tmpl').html();
var emptyComments=comments.emptyComments();
var html=emptyCommentsTmpl(emptyComments.toJSON());
$(this.el).html(html);
},
});
var comments=新注释();
var commentView=新的commentView({
收藏:评论
});
var emptyCommentView=新的emptyCommentView({
收藏:评论
});
comments.fetch({/=MESSAGE\u LIMIT\u LENGTH){
model.set(“预览消息”,消息substr(0,消息限制长度/2));
}
返回真值
}
返回错误
});
返回新的评论(模型);
},
emptyComments:function(){
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
返回false==(msg).notBlank();
});
返回新的评论(模型);
}
});
var CommentView=Backbone.View.extend({
el:$(“#评论#部分”),
模板:commentsTmpl,//模板引用,最好在这里创建
初始化:函数(){
this.render();//自呈现
},
render:function(){
如果(this.collection.length){//请使用this.collection引用视图的集合,而不是外部变量
$(“#dadasay_comments_plugin”).show();//这不应该是全局选择
}
var html=this.template(this.collection.toJSON());
这个.$el.append(html);
//---^使用缓存的jQuery对象,而不是创建新对象
},
});
var EmptyCommentView=Backbone.View.extend({
el:$(“空注释列表”),
模板:emptyCommentsTmpl,
初始化:函数(){
这个。render();
},
render:function(){
var source=$('#空的(注释)列表(tmpl').html();//未使用?
var html=this.template(this.collection.toJSON());
这个.$el.html(html);
},
});
var comments=新注释();
comments.fetch({//问题是您的comments集合在初始化时触发fetch
。它的方法wellFormedComments
和emptyComments
创建新的comments集合,以便它们也触发fetch
您可以通过在需要时手动触发fetch来解决此问题,例如:
var Comments=Backbone.Collection.extend({
模式:评论,
url:fetch_comments_url,
wellFormedComments:function(){
var消息限制长度=80
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
如果(msg!=null){
msg=msg.replace(/^\s+|\s+$/g'))
如果(msg.length>=消息\u限制\u长度){
model.set(“预览消息”,消息substr(0,消息限制长度/2));
}else{};
返回真值
}否则{
返回错误
};
});
返回新的评论(模型);
},
emptyComments:function(){
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
返回false==(msg).notBlank();
});
返回新的评论(模型);
}
});
var CommentView=Backbone.View.extend({
el:$(“#评论#部分”),
render:function(){
var notNullComments=comments.wellFormedComments();
如果(notNullComments.length>0){
$(“#dadasay_comments_plugin”).show();
}
var html=commentsTmpl(notNullComments.toJSON());
$(this.el).append(html);
},
});
var EmptyCommentView=Backbone.View.extend({
el:$(“空注释列表”),
render:function(){
var source=$('#空的#comments_list_tmpl').html();
var emptyComments=comments.emptyComments();
var html=emptyCommentsTmpl(emptyComments.toJSON());
$(this.el).html(html);
},
});
var comments=新注释();
var commentView=新的commentView({
收藏:评论
});
var emptyCommentView=新的emptyCommentView({
收藏:评论
});
comments.fetch({/=MESSAGE\u LIMIT\u LENGTH){
model.set(“预览消息”,消息substr(0,消息限制长度/2));
}
返回真值
}
返回错误
});
返回新的评论(模型);
},
emptyComments:function(){
var模型=此。选择(函数(模型){
var msg=model.get(“消息”)
返回false==(msg).notBlank();
});
返回新的评论(模型);
}
});
var CommentView=Backbone.View.ext
var Comments = Backbone.Collection.extend({
model: Comment,
url: fetch_comments_url,
initialize: function() {
this.fetch({
success: this.fetchSuccess,
error: this.fetchError
});
this.deferred = new $.Deferred();
},
deferred: Function.constructor.prototype,
fetchSuccess: function(collection, response) {
collection.deferred.resolve();
},
fetchError: function(collection, response) {
throw new Error("Products fetch did get collection from API");
},
var comments = new Comments();
...
comments.deferred.done(function() {
commentView.render();
emptyCommentView.render();
});
var Comments = Backbone.Collection.extend({
model: Comment,
url: fetch_comments_url,
initialize: function() {
this.fetch({
success: this.fetchSuccess,
error: this.fetchError
});
this.deferred = new $.Deferred();
},
deferred: Function.constructor.prototype,
fetchSuccess: function(collection, response) {
collection.deferred.resolve();
},
fetchError: function(collection, response) {
throw new Error("Products fetch did get collection from API");
},
wellFormedComments: function () {
var MESSAGE_LIMIT_LENGTH = 80
var models = comments.select(function (model) {
var msg = model.get("message")
if (msg!=null) {
msg = msg.replace(/^\s+|\s+$/g, '')
if (msg.length >= MESSAGE_LIMIT_LENGTH) {
model.set("preview_message", msg.substr(0, MESSAGE_LIMIT_LENGTH/2));
} else{
};
return true
}
else{
return false
};
});
return new Comments(models);
},
emptyComments: function () {
var models = comments.select(function (model) {
var msg = model.get("message")
return false===_(msg).notBlank();
});
return new Comments(models);
}
});
var comments = new Comments();
var CommentView = Backbone.View.extend({
el: $("#comments_section"),
render: function() {
var notNullComments = comments.wellFormedComments();
if (notNullComments.length > 0) {
$("#dadasay_comments_plugin").show();
}
var html = commentsTmpl(notNullComments.toJSON());
$(this.el).append(html);
},
});
var EmptyCommentView = Backbone.View.extend({
el: $("#empty_comments_list"),
render: function() {
var source = $('#empty_comments_list_tmpl').html();
var emptyComments = comments.emptyComments();
var html = emptyCommentsTmpl(emptyComments.toJSON());
$(this.el).html(html);
},
});
var commentView = new CommentView({
collection: comments
});
var emptyCommentView = new EmptyCommentView({
collection: comments
});
comments.deferred.done(function() {
commentView.render();
emptyCommentView.render();
});