Marionette.js EmptyChildView

时间:2015-04-24 12:57:33

标签: javascript backbone.js marionette

这是我的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(它返回undefined)

1 个答案:

答案 0 :(得分:1)

使用视图数据作为上下文(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