重新加载主干.在Ajax请求后查看.怎样

Reload Backbone.View after Ajax request.How?

本文关键字:请求 Ajax 怎样 新加载 加载      更新时间:2023-09-26

我在Backbone.Viewchange集合中有ajax请求:

change: function(model, options){
me = this;
$.ajax({
    url: ajaxurl,
    type: 'post',
    data: {
        action: 'getShortcode',
        shortcode: model.attributes.shortcode
    },
    success: function (data) {
       me.$el.find('section:eq(' + model.collection.indexOf(model) + ')').replaceWith(data);
       me.add( model, model.collection );
    }
});
}

但是在ajax请求之后,我的另一个Backbone.View(通过触发器调用此ajax)无法再次工作,如何在ajax之后"重新加载"我的另个Backbone.View,以强制触发器再次调用ajax?thx!

您应该使用Backbone.Model。如果您的模型已经是Backbone.Model,只需使用model.set( anObjectWithTheChanges ),就会在模型中触发change事件。

然后,只需在视图的initialize函数中添加一些代码,即可听取模型中的更改。类似于:

this.listenTo( this.model, 'change', this.render);

其中render是您视图中负责刷新的方法。