2015-04-24 7 views
1

Вот мой EmptyChildView:Marionette.js 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 (она возвращает неопределенное значение)

ответ

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

+0

Спасибо !!!!!!! – dennismonsewicz

Смежные вопросы