简体   繁体   English

函数_.bind()不会绑定对象

[英]Function _.bind() won't bind an object

I have a view class extending SugarCRM CreateView and I want this to be this.model in function checkMonths when the field starting_months_c is changed, so I could type this.get() instead of this.model.get() . 我具有延伸SugarCRM的视图类CreateView ,我想thisthis.model在功能checkMonths当字段starting_months_c被改变,因此我可以输入this.get()代替this.model.get()

/**
 * @class View.Views.Base.DoVPCreateView
 * @alias SUGAR.App.view.views.DoVPCreateView
 * @extends View.Views.Base.CreateView
 */
({
    extendsFrom: 'CreateView',
    initialize: function(options) {
        this._super('initialize', arguments);
        // ....
        this.model.on('change:starting_month_c', _.bind(this.checkMonths, this.model));
        // ....
    },
    checkMonths: function() {
        if (this.get('starting_month') == 12) {
            // ....
        }
    }

Unfortunately, this construct does not work. 不幸的是,这种构造不起作用。 I wonder, maybe it is because the .on() function somehow sets the context itself? 我想知道,也许是因为.on()函数以某种方式设置了上下文本身吗?

I found out in the doc, that you can pass the context to the function as third parameter 我在文档中发现,您可以将上下文作为第三个参数传递给函数

object.on(event, callback, [context])

I tried this but the result is still the same - the view is this , not the model . 我试过了,但是结果还是一样的-视图就是this ,而不是model

Quick fix 快速解决

Give the context directly to .on : 将上下文直接提供给.on

this.model.on('change:starting_month_c', this.checkMonths, this.model);

But doing this is only a misleading fix. 但这只是一个误导性的修复。 The view's functions should all have this being the view instance and not other arbitrary objects. 视图的功能都应该有this作为视图实例,而不是其他任意对象。

 // a simple example view var View = Backbone.View.extend({ initialize: function() { console.log("View init, month:", this.model.get('month')); // bind the context this.model.listenTo(this.model, "change:month", this.checkMonth); }, // the callback checkMonth: function() { // here, `this` is the model which you should NOT do. // but for demonstration purpose, you can use `this.get` directly. console.log("checkMonth:", this.get('month')); }, }); // sample for the demo var model = new Backbone.Model({ month: 2 // dummy value }), view = new View({ model: model }); console.log("change month"); model.set({ month: 3 // set to trigger the callback }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script> 

Real fix 真正的修复

If you always want to trigger a check "months" callback whenever starting_month_c changes in any instance of this model, you could move that into the model class itself. 如果在该模型的任何实例中,无论starting_month_c何时发生更改,如果您始终希望触发检查“ months”回调,则可以将其移入模型类本身。

var Model = Backbone.Model.extend({
    initialize: function() {
        // favor listenTo over `on` or `bind`
        this.listenTo(this, 'change:starting_month_c', this.checkMonths);
    },
    checkMonths: function(model, value, options) {
        if (this.get('starting_month') === 12) {
            // whatever you want
        }
    }
});

If it's only for this specific view, use this.model.get in the callback as it should be . 如果仅用于此特定视图, this.model.get在回调中使用this.model.get This is not a problem, it's the standard way of doing it. 这不是问题,这是标准的做法。

More info on why to favor listenTo . 有关为何偏爱listenTo更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM