javascript - Marionette.js EmptyChildView

标签 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 将成为多个 View 的共享组件。由于某种原因,我无法访问 templateHelpers 中的 this.link (它返回未定义)

最佳答案

templateHelpers 函数以 View 数据作为上下文 (this) 进行调用。您需要将 link 添加到序列化 View 数据:

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/

相关文章:

javascript - 在网站中实现 Google Places Api

javascript - 无法使用 attr 绑定(bind)在嵌入标记中绑定(bind) src 属性

javascript - 是什么让 Angular 和 Backbone 与 jQuery 不同?

marionette - Backbone Marionette 事件聚合器 : how to listen at the App level to ItemView Event

javascript - 在 Marionette 中从另一个应用程序调用一个应用程序的 View

django - 如何部署连接到 Django RESTful API 的静态网站?

javascript - 没有 JSON 根的 Ember.js REST 适配器

javascript - jQuery - 每次滚动时 scrollTop() 求和

javascript - 主干.集合.创建

javascript - 使用backbone.js获取集合,但未设置每个模型的id