如何从一个项目视图实例访问一个复合视图骨干木偶 [英] How to access a composite view from an item view instance in Backbone Marionette

查看:99
本文介绍了如何从一个项目视图实例访问一个复合视图骨干木偶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本情况是这样的:

我有一个复合视图和项目视图。我构建传递一个模型,并收集综合视图。该模型数据被用于填充复合视图的模板。收集的数据被用于填充复合视图项目视图。

I have a Composite View and an Item View. I construct the Composite view passing it a model and a collection. The model data is used to populate the template for the Composite view. The collection data is used to populate the Item View for the Composite View.

我想要做的是这样的:在一个模板帮手项目视图我想要访问的模型数据的复合视图。至于访问项目视图的视图实例我已经得到了。我想这可能给我的复合视图,从那里我能得到它的模型的句柄,但事实并非如此。

What I want to do is this: in a template helper for the Item view I want to access the model data for the Composite View. I've got as far as accessing the view instance of the Item View. I thought that might give me a handle on the Composite View, from where I could get to its model, but it doesn't.

有没有一种方法,我可以做到这一点 - 从项目视图实例之一访问复合视图实例

Is there a way I can do this - access the composite view instance from one of its item view instances?

感谢

- 贾斯汀威利

推荐答案

如果您想从父访问数据 CompositeView中的你可以做许多不同的事情。

If you want to access data from the parent CompositeView you can do a number of different things.


  1. 直接传递数据给 ItemView控件通过关于<$ c中的 itemViewOptions 辅助函数$ C> CompositeView中的。注:此选项已木偶2改为 childViewOptions

  1. Either pass that data directly to the ItemView through the itemViewOptions helper function on the CompositeView. Note: This option has changed to childViewOptions in Marionette 2.

直接调用一个方法上所有的孩子从 CompositeView中的查看,并通过任何你想要进入的方法。

Invoke a method directly on all of the children view from the CompositeView and pass whatever you want into that method.

在触发一个事件或听到的 ItemView控件

Trigger an event on or listened for by the ItemView.

这些选项都不是直接访问从孩子家长的看法,但应该做你想要什么。下面是code如何使用这两种方法在 CompositeView中的的模式传递给孩子们观看。

None of these options are directly accessing the parent view from the child but should do what you want. Below is code for how to use each of these approaches to pass the CompositeView's model to the children view.

// Pass model into ItemView on init
var MyItemView = Backbone.Marionette.ItemView.extend({
  initialize : function (options) {
    this.parentsModel = options.parentsModel;
  }
});
var MyCompView = Backbone.Marionette.CompositeView.extend({
  itemViewOptions : function () { return { parentsModel: this.model }; }
  itemView : MyItemView
});


// Invoke function on ItemView, passing data in
var MyItemView = Backbone.Marionette.ItemView.extend({
  doSomethingWithParent : function (parentModel) {
    // do cool thing with parentModel
  }
});
var MyCompView = Backbone.Marionette.CompositeView.extend({
  itemView : MyItemView,
  onRender : function () {
    this.children.call("doSomethingWithParent", this.model);
  }
});


// Trigger event that ItemView knows about
var MyItemView = Backbone.Marionette.ItemView.extend({
  initialize : function () {
    this.listenTo(this, "special:event", function (parentModel) {
      // Do cool things
    });
  }
});
var MyCompView = Backbone.Marionette.CompositeView.extend({
  itemView : MyItemView,
  onRender : function () {
    this.children.each(_.bind(function (childView) {
      childView.trigger("special:event", this.model);
     }, this));
  }
});

这篇关于如何从一个项目视图实例访问一个复合视图骨干木偶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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