在另一Marionette.ItemView调用函数 [英] Call a function in another Marionette.ItemView

查看:112
本文介绍了在另一Marionette.ItemView调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ItemView控件,在这里我使用clearSearch()函数。我需要调用在另一ItemView控件相同的功能,所以要保持干燥席力图召clearSearch(),但我没有工作。

I have one ItemView, where I use clearSearch() function. I need to call the same function in another ItemView, so to keep it DRY I tried to call clearSearch(), but i didn't work.

View.Panel = Marionette.ItemView.extend({
    template: panelTpl,
    events: {
        'click .search_clear': 'clearSearch',
    }
    clearSearch: function() {
        //some important actions
    }
});

View.Pagination = Marionette.ItemView.extend({
    template: paginationTpl,
    events: {
        'click .page': 'changePage'
    },
    changePage: function(e) {
        //others important actions
        clearSearch();
    }
});

我也试过用 View.Panel.clearSearch(),但我得到这个错误:

未捕获类型错误:目标函数(){返回i.apply(这一点,参数)}
  没有方法'clearSearch

Uncaught TypeError: Object function (){return i.apply(this,arguments)} has no method 'clearSearch'

推荐答案

使用事件。

定义一个全局的事件总线:

define a global event bus:

Event.Dispatcher = _.clone(Backbone.Events);

和在你的分页视图。

View.Pagination = Marionette.ItemView.extend({
  template: paginationTpl,
  events: {
    'click .page': 'changePage'
  },
  changePage: function(e) {
    //notify page change event
    Event.Dispatcher.trigger("pageChanged", [pass any data you want to pass]);
  }
});

在您的面板视图,收听本次活动,并确定如何处理它。

in your panel view, listen to this event, and define how to handle it.

View.Panel = Marionette.ItemView.extend({
  template: panelTpl,
  events: {
    'click .search_clear': 'clearSearch',
  },
  initialize: function() {
    //listen to that event
    this.listenTo(Event.Dispatcher, 'pageChanged', this.clearSearch);
  },
  clearSearch: function() {
    //some important actions
  }
});

我没有与任何木偶经验。可能有木偶来实现这个更简单的方法,但是这是我一直在用我的纯香草骨干应用的模式。

I don't have any experience with Marionette. There may be easier ways to implement this with Marionette, but this is the pattern I've been using in my pure vanilla backbone applications.

这篇关于在另一Marionette.ItemView调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆