简体   繁体   English

Backbone Marionette:我可以为collectionView的emptyView提供选项吗?

[英]Backbone Marionette: can I supply options to a collectionView's emptyView?

The scenario is this: I have a collectionView that gets used in a couple of places. 场景是这样的:我有一个在几个地方使用的collectionView I pass a few options into the view to change certain display aspects (verbiage mostly), since the behavior is exactly the same everywhere. 我将一些选项传递到视图中以更改某些显示方面(主要是措辞),因为到处都是完全相同的行为。

I'd really like to extend this customization to the emptyView , but I can't find a way to do so. 我真的想将此自定义扩展到emptyView ,但我找不到这样做的方法。 There seems to be no reference to the collectionView on the emptyView , and neither can I seem to access the emptyView from the collectionView , outside of defining it. 似乎没有对emptyView上的collectionView引用,除了定义它之外,我似乎也无法从collectionView访问emptyView

Basically, I'd like to be able to do something like this: 基本上,我希望能够做到这样的事情:

var noItemsView = Backbone.Marionette.ItemView.extend({
    tagName: "li",
    className: "no-results",
    template: Handlebars.compile(noResultsTemplate),
}),

leftToggleListView = Backbone.Marionette.CollectionView.extend({
    tagName: "ul",
    className: "left-toggle-view-list",
    emptyView: noItemsView,

    initialize: function() {
        this.emptyView.model.set("name": "some custom name");
    }
});

And then have the noItemsView be able to render {{ name }} within its template. 然后让noItemsView能够在其模板中呈现{{ name }}

Is there any way to accomplish this, short of modifying Marionette? 有没有办法实现这一点,没有修改木偶?

In the collectionView you can use the buildItemView, this function will be called also at the time to build the emptyView 在collectionView中你可以使用buildItemView,这个函数也会在构建emptyView时被调用

I did a little demo in jsFiddle http://jsfiddle.net/rayweb_on/TN34P/ 我在jsFiddle http://jsfiddle.net/rayweb_on/TN34P/做了一个小小的演示

var leftToggleListView = Backbone.Marionette.CollectionView.extend({
tagName: "ul",
className: "left-toggle-view-list",
emptyView: noItemsView,
ValuethatMakesSense : "I do!",
buildItemView: function(item, ItemViewType, itemViewOptions){
   var options = _.extend({model: item}, itemViewOptions);
   var name = this.ValuethatMakesSense;
   var view = new ItemViewType({name : name});
   return view;
}
});

And in the initialize function of your item view you can read the options passed. 在项目视图的初始化功能中,您可以阅读传递的选项。

var noItemsView = Backbone.Marionette.ItemView.extend({
initialize : function (options) {
  var name = this.options.name;
  console.log(name);
},

tagName: "li",
className: "no-results",
template: "#noresults"
});

Im using a property inside the collectionView and then reading it/passing it to the empty view in the buildItemView just to test the functionality of the buildItemView function, you can do the proper logic checks and validations there. 我在collectionView中使用一个属性然后读取它/将它传递给buildItemView中的空视图只是为了测试buildItemView函数的功能,你可以在那里进行适当的逻辑检查和验证。

It was a while since this question was asked, but it might be helpful to anybody looking: 自从提出这个问题以来已经有一段时间了,但对任何人来说这可能会有所帮助:

Marionette CollectionView have an emptyViewOptions property now that works exactly like itemViewOptions but for emptyView: Marionette CollectionView现在有一个emptyViewOptions属性,其工作方式与itemViewOptions类似,但对于emptyView:

http://marionettejs.com/docs/v2.4.2/marionette.collectionview.html#collectionviews-emptyviewoptions http://marionettejs.com/docs/v2.4.2/marionette.collectionview.html#collectionviews-emptyviewoptions

Actually you can access to the emptyView in this way: 实际上你可以这样访问emptyView:

this.children._views[_.keys(this.children._views)[0]];

Seems like in the new version we will have a method that allows to get an emptyView . 似乎在新版本中我们将有一个允许获取emptyView https://github.com/marionettejs/backbone.marionette/pull/727 https://github.com/marionettejs/backbone.marionette/pull/727

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

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