0

在 newAttributes 方法内的 formView 中,我想触发一个事件,然后显示条目视图(两者都显示在这里)。为了做到这一点,我应该使用控制器吗?似乎无论我如何设置 trigger 和 listenTo 命令,我都会遇到不同的错误。

MyApp = new Backbone.Marionette.Application();

MyApp.addRegions({
    formBox : '#formBox',
    listBox : '#listBox'
});

Entry = Backbone.Model.extend({
    defaults: {
        DB : 'not specified',
        CT : 'not specified',
        go : 'not specified'
    },
});

EntryList = Backbone.Collection.extend({
    model: Entry
});

FormView = Backbone.Marionette.ItemView.extend({
    tagName: 'div',
    template: '#form-template',
    className: 'form',

    ui: {
        DB: '#DB',
        CT: '#CT',
        gos: '#gos'
    },

    events:{
        'click #processInput' : 'validateForm'
    },

    onRender: function(){
        console.log("rendering...");
    },

    validateForm : function(){
        var validInput = true;

        if(!this.ui.DB.val().trim())
        {
            validInput = false;
        !this.ui.DB.css("background-color","#CC0000");  
        }
        else
        {
            !this.ui.DB.css("background-color","#ffffff");
        }
        if(!this.ui.CT.val().trim())
        {   
            validInput = false;
        this.ui.CT.css("background-color","#CC0000");
        }
        else
        {
            this.ui.CT.css("background-color","#ffffff");
        }
        if(!this.ui.gos.val().trim())
        {
            validInput = false;
        this.ui.gos.css("background-color","#CC0000");
        }
        else
        {
            this.ui.gos.css("background-color","#ffffff");
        }

        if(validInput)
        {
            this.hideForm();
        this.getEntries(this.ui.DB.val().trim(),
        this.ui.CT.val().trim(),this.ui.gos.val().trim());
        }
    },

    newAttributes :function(db,con,gos){
        for(go in gos)
        {
            MyApp.ents.add({DB : db, CT: con, go: gos[go]});
        }
        //TRIGGER CUSTOM EVENT
    },


    getEntries : function(db,con,gos){
        var gos = gos.split("\n");
        for(go in gos)
        {
            console.log("data bank: " + db + " CT: " + con + " go: |" +                                                                                                                                                  gos[go].trim()+"|");
        }   
            this.newAttributes(db,con,gos);
    },

    hideForm : function(){
        this.$el.hide();
    }
});

EntryView = Backbone.Marionette.ItemView.extend({
    tagName: 'tr',
    template: '#entry-template',
    className: 'entry',

    events: {
        'click .delete' : 'destroy'
    },

    destroy : function(){
        this.model.destroy();
    }
});

EntriesView = Backbone.Marionette.CompositeView.extend({
    tagName: 'table',
    template: '#entries-template',
    itemView: EntryView,

    appendHtml: function(collectionView, itemView){
            colleCTionView.$('tbody').append(itemView.el);      
    }
});

MyApp.addInitializer(function(test){     
    var entriesView = new EntriesView({
          collection: test.DB
    });

    var formView = new FormView();

    //SHOW on load
    MyApp.formBox.show(formView);  
    //SHOW Later with custom event
    MyApp.listBox.show(entriesView)
    });

    $(document).ready(function(){
      MyApp.ents = new EntryList([]);
      MyApp.start({DB: MyApp.ents});
    });
4

1 回答 1

0

有两种方法可以解决您的问题。

最干净的方法是使用控制器,并在控制器内的视图实例上监听触发的事件。例如,在您看来, have this.trigger("my:event"),然后在您的控制器中 havemyFormViewInstance.on("my:event", function(...){...})

另一种选择(实际上并不推荐)是在应用程序级别触发事件,并在应用程序级别的其他地方监听它。即MyApp.trigger("my:event")MyApp.listen("my:event")

于 2013-07-09T21:05:41.550 回答