Javascript 正确的方法
我的主干应用程序有一个名为schedule的视图,我对调用正确函数的成功和错误的区别有点困惑,我尝试了下面列出的两种可能的方法,但我不知道区别是什么,从外部视图中放置的路由器调用函数的正确方法是什么: 第一种方式:Javascript 正确的方法,javascript,backbone.js,amd,Javascript,Backbone.js,Amd,我的主干应用程序有一个名为schedule的视图,我对调用正确函数的成功和错误的区别有点困惑,我尝试了下面列出的两种可能的方法,但我不知道区别是什么,从外部视图中放置的路由器调用函数的正确方法是什么: 第一种方式: require([ 'app/collections/schedule', 'app/views/schedule' ], function(ScheduleCollection, ScheduleVie
require([
'app/collections/schedule',
'app/views/schedule'
], function(ScheduleCollection, ScheduleView) {
var scheduleCollection = new ScheduleCollection(),
scheduleView = new ScheduleView({
model: scheduleCollection
});
scheduleCollection.fetch({
reset: true,
success: function(){
scheduleView.successHandler();
},
error: function(){
scheduleView.errorHandler()
}
});
});
第二条路
require([
'app/collections/schedule',
'app/views/schedule'
], function(ScheduleCollection, ScheduleView) {
var scheduleCollection = new ScheduleCollection(),
scheduleView = new ScheduleView({
model: scheduleCollection
});
scheduleCollection.fetch({
reset: true,
success: scheduleView.successHandler(),
error: scheduleView.errorHandler()
});
});
在scheduleView中
successHandler: function(){
console.log('success');
}
erroHandler: function(){
console.log('error');
}
第一种方法是正确的,第二种方法是错误的。但这种方式最为简洁:
scheduleCollection.fetch({
reset: true,
success: scheduleView.successHandler,
error: scheduleView.errorHandler
});
(虽然…从OP中,函数
scheduleView.successHandler
不存在,因此这可能是一个问题。)您需要做的是将函数对象分配给“success”和“error”,而不是函数的返回值。
当您这样做时:
function(){...}
它返回一个函数对象,因此
成功:函数(){…}
是对的,但如果
a = function(){...}
然后执行a()
它执行函数并返回返回值,因此
success:a()
var foo = myFunction(); // foo is set to myFunction's return value
var bar = myFunction; // bar is set to a REFERENCE to myFunction
foo === bar // FALSE
bar === myFunction // TRUE
更正代码:
scheduleCollection.fetch({
reset: true,
success: scheduleView.successHandler,
error: scheduleView.errorHandler
});
现在,如果您想获得高级,那么使用返回的XHR对象的off在各个方面都是优越的,而且回调应该不再是必要的
scheduleCollection.fetch({ reset: true })
.then(scheduleView.successHandler, scheduleView.errorHandler);
不同的是你得到了一个可以传递的承诺。。。但这是另一篇文章的主题。(无耻插件)查看我关于承诺的三部分系列。…还有另一个选择:与其直接引用视图,不如提供集合作为相关视图的引用,并收听相关事件。例如,在相关视图中侦听集合的重置。如果您不想挂接该事件,则从视图可以侦听的成功/错误回调中触发一个自定义事件
下面是一个处理重置的示例-扩展ScheduleView:
var ScheduleView = Backbone.View.extend({
initialize: function () {
this.listenTo(this.collection, 'reset', this.handleReset);
},
handleReset: function () {
// do whatever you need to do here
}
};
var scheduleCollection = new ScheduleCollection();
var scheduleView = new ScheduleView({ collection: scheduleCollection });
以下是与集合中的成功/错误处理程序关联的自定义事件示例:
var ScheduleCollection = Backbone.Collection.extend({
getResults: function () {
var self = this;
this.fetch({
reset: true,
success: function (collection, response, options) {
// you can pass additional options to the event you trigger here as well
self.trigger('successOnFetch');
},
error: function (collection, response, options) {
// you can pass additional options to the event you trigger here as well
self.trigger('errorOnFetch');
}
});
}
};
var ScheduleView = Backbone.View.extend({
initialize: function () {
this.listenTo(this.collection, 'successOnFetch', this.handleSuccess);
this.listenTo(this.collection, 'errorOnFetch', this.handleError);
},
handleSuccess: function (options) {
// options will be any options you passed when triggering the custom event
},
handleError: function (options) {
// options will be any options you passed when triggering the custom event
}
};
var scheduleCollection = new ScheduleCollection();
var scheduleView = new ScheduleView({ collection: scheduleCollection });
scheduleCollection.getResults();
以这种方式连接的优点是可以删除集合对视图的依赖关系。如果您想让多个视图侦听集合(或您的集合模型)上发生的事件,并且是主干应用程序的松散耦合体系结构,这一点尤其重要。请提供一个示例Bravo,详细说明这一点,这正是我所需要的!您是否需要将这些方法绑定到scheduleView
?不是在console.log
案例中,但是如果这些方法使用了this
。@nrabinowitz是的,我相信是这样的。。。我一直都不太清楚,我真的很喜欢promise解决方案,代码从左到右读起来很漂亮。我将采用jQuery承诺作为骨干网获取等方面的最佳实践。