这是我的EmptyChildView:

define(['marionette', 'underscore',
'text!components/empty-options-view/template.html', 'config'],
function(Marionette, _, templateHTML, Config) {
    'use strict';

    var EmptyOptionsView = Marionette.ItemView.extend({
        template: _.template(templateHTML),
        className: 'empty-options',

        initialize: function(options) {
            this.link = options.url;
        },

        templateHelpers: function() {
            return {
                externalLink: function() {
                    return Config.get('base_url') + this.link;
                }
            };
        }
    });

    return EmptyOptionsView;
});


这是我的使用方式:

define(['marionette', 'groups-menu/groups/item-view', 'components/empty-options-view/view',
'eventer'],
function (Marionette, GroupItemView, EmptyOptionsView) {
    'use strict';

    var GroupsCollectionView = Marionette.CollectionView.extend({
        childView: GroupItemView,
        emptyView: EmptyOptionsView,

        emptyViewOptions: {
            url: '/settings'
        },
        /**
         * Toggles the "all" radio button on, unchecks all individual signup
         * checkboxes.
         * @method GroupsCollectionView.markAll
         */
        markAll: function () {
            this.collection.uncheckAll();
        }
    });

    return GroupsCollectionView;
});


EmptyView将成为多个视图的共享组件。由于某些原因,我无法访问我的this.link中的templateHelpers(它返回未定义)

最佳答案

使用视图数据作为上下文(templateHelpers)调用this函数。您需要将link添加到序列化的视图数据中:

var EmptyOptionsView = Marionette.ItemView.extend({
  // ...

  serializeData: function() {
    return _.extend(this.model.toJSON(), {
      link: this.link;
    }
  },

  templateHelpers: function() {
    return {
      externalLink: function() {
        return Config.get('base_url') + this.link;
      }
    }
}

// ...


}

Marionette Docs - Accessing data within view helpers

关于javascript - Marionette.js EmptyChildView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29848151/

10-10 06:35