1

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

这在主干.js 脚本的第 128 行中有说明,但我不知道他在那里做什么。看起来他正在寻找集合上的“to”或“on”事件。

我在这里做错了什么?

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);
    }
});
4

1 回答 1

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);
}, 
于 2013-06-27T01:36:03.033 回答