在 Marionette 中从另一个应用调用一个应用的视图

Call one app's view from another in Marionette

我有一个 IdentificationView 必须调用不同应用程序中另一个视图的方法:

var NameAndIdentificationView = Application.Views.ItemView.extend({
    loadMailingAddressView: function() {
                //call to mailing address view method setAddress() from here
    }
});

现在我的 MailingAddressView 是这样的:

var MailingAddressView= Application.Views.ItemView.extend({
    setAddress: function(address) {
        if (address != null) {
            // this.autoCompleteBox.data('inputData') = address;
            ProWeb.EXTRACTED_ADDRESS = address;
            this.model.set('mailingAddress', address);
        }
    }
});

我想知道如何从一个视图调用另一个视图的方法。请提供建议。

编辑:

我的应用程序是这样设置的:

  1. 首先调用 App
  2. 然后调用 控制器
  3. Controller 内部调用并初始化 View

因此,在我的 NameIdentificationView 中,我正在调用 MailingAddress 应用程序,它反过来控制调用控制器和这是观点。

您可以通过多种方式使用事件通道、在共享模型上触发和侦听事件,更简单的方法是使用 Bsckbone 事件。

在来电者中触发一个事件

loadMailingAddressView: function () {
                //call to mailing address view method setAddress() from here
             Backbonw.trigger('setAddress')
})

并监听被调用者的初始化

    var MailingAddressView = Application.Views.ItemView.extend({

    initialize: function() {
        Backbone.on('setAddress', this.setAddress)
    },
    setAddress: function(address) {
        if (address != null) {
            // this.autoCompleteBox.data('inputData') = address;
            ProWeb.EXTRACTED_ADDRESS = address;
            this.model.set('mailingAddress', address);
        }
    },
});

确保被调用者视图在您触发事件时已初始化