控制器listenTo在回调方法中中断

时间:2013-06-26 12:20:52

标签: backbone.js marionette backbone-events

我遇到的问题是事件“form:selectedForm”正在调用方法“showForm”,但是当我将它发送到我的视图时,我收到以下错误:TypeError:e [t]不是函数。

这在backbone.js脚本的第128行中有说明,但我不知道他在那里做了什么。看起来他正在寻找收藏品上的“到”或“开”事件。

我在这里做错了什么?

MyController = Backbone.Marionette.Controller.extend({

initialize: function(options) {
    this.options = options;
    this.urls = options.urls;
    this.mainRegion = options.mainRegion;

    this.view  = new MyLayout();
    this.mainRegion.show(this.view);
    this.view.render();

    this.showSelectorView(this.view.formHeader);
},

showSelectorView : function(view) {
    var forms = new MyForms();
    forms = this.urls.loadForms;

    var selectorView = new FormSelectorView({
        collection: forms 
    });
    forms.fetch();
    this.listenTo(selectorView, "form:selectedForm", this.showForm);

    view.show(selectorView);
},

showForm : function(models) {
    console.log("showForm");
    var form = new FormContentView({
        collection: models
    });
        this.view.form.show(form);
    }
});

MyLayout = Backbone.Marionette.Layout.extend({

    template: Backbone.Marionette.TemplateCache.get('#content'),

    regions: {
        formHeader:         "#selector",
        form:           "#formContent",
        formContent:        "#content",
        formFooter:         "#save",
        formTemplates:      "#templates"
    }

});

FormSelectorView = Backbone.Marionette.ItemView.extend({

    template: Backbone.Marionette.TemplateCache.get('form-selector-template'),

    events : {
        "click option" : "selectForm"
    },

    initialize : function() {
        this.listenTo(this.collection, "sync", this.render, this);
    },

    selectForm : function(e) {
         e.preventDefault();
         var id = $(e.currentTarget).attr("name");
         var item = this.collection.get(id);

         this.trigger("form:selectedForm", item.attributes.fields);
    }
});

1 个答案:

答案 0 :(得分:1)

我认为错误在你的showSelector视图函数中,你在第二行覆盖你的表单集合,

我认为你在这一行中的意图是确定表单集的url所以我的猜测是这将解决它:

showSelectorView : function(view) {
var forms = new MyForms();
forms.url = this.urls.loadForms;   /// Im assuming you were trying to pass the url here

var selectorView = new FormSelectorView({
    collection: forms 
});
forms.fetch();
this.listenTo(selectorView, "form:selectedForm", this.showForm);

view.show(selectorView);
},