如何使用自定义函数调用主干模型事件



如何使用我在代码中描述的自定义函数调用主干事件?我正在使用自己的自定义函数doNowClick()调用主干事件。我这么做是因为我无法在不同的帖子上触发相同的事件。

(function(AE, $, Backbone, Views, Models, Collections) {
		  Views.PostItem = Backbone.Marionette.ItemView.extend({
        // view html tag
        tagName: "li",
        // view class
        className: 'col-md-3 col-xs-6 place-item ae-item',
        /**
         * view events
         */
        events: {
            // user click on action button such as edit, archive, reject
            'click a.action': 'acting'
        },
        /**
         * list all model events
         */
        modelEvents: {
            "change": "modelChanged",
            "change:post_status": "statusChange"
        },
        /**
         * model in view change callback function
         * update model data to database
         */
        modelChanged: function(model) {
            this.render();
        },
        statusChange: function(model) {
            AE.pubsub.trigger('ae:model:statuschange', model);
        },
        /**
         * catch event on view render bind raty for star rating
         */
        // onRender: function() {
        //     var view = this;
        //     this.$('.rate-it').raty({
        //         half: true,
        //         score: view.model.get('rating_score'),
        //         readOnly: true,
        //         hints: raty.hint
        //     });
        // },
        /**
         * event callback when user click on action button
         * edit
         * archive
         * reject
         * toggleFeatured
         * approve
         */
        acting: function(e) {
            e.preventDefault();
            var target = $(e.currentTarget),
                action = target.attr('data-action'),
                view = this;
            switch (action) {
                case 'edit':
                    //trigger an event will be catch by AE.App to open modal edit
                    AE.pubsub.trigger('ae:model:onEdit', this.model);
                    break;
                case 'reject':
                    //trigger an event will be catch by AE.App to open modal reject
                    AE.pubsub.trigger('ae:model:onReject', this.model);
                    break;
                case 'archive':
                    if (confirm(ae_globals.confirm_message)) {
                        // archive a model
                        this.model.set('archive', 1);
                        this.model.save('archive', '1', {
                            beforeSend: function() {
                                view.blockItem();
                            },
                            success: function(result, res, xhr) {
                                view.unblockItem();
                            }
                        });
                    }
                    break;
                case 'toggleFeature':
                    // toggle featured
                    this.model.save('et_featured', 1);
                    break;
                case 'approve':
                    // publish a model
                    this.model.save('publish', '1', {
                        beforeSend: function() {
                            view.blockItem();
                        },
                        success: function(result, res, xhr) {
                            view.unblockItem();
                        }
                    });
                    break;
                case 'delete':
                    if (confirm(ae_globals.confirm_message)) {
                        // archive a model
                        var view = this;
                        this.model.save('delete', '1', {
                            beforeSend: function() {
                                view.blockItem();
                            },
                            success: function(result, res, xhr) {
                                view.unblockItem();
                                if(res.success){
                                    view.model.destroy();    
                                }                                
                            }
                        });
                    }
                    break;
                default:
                    //trigger an event will be catch by AE.App to open modal edit
                    AE.pubsub.trigger('ae:model:on' + action, this.model);
                    break;
            }
        },
        /**
         * load block item
         */
        blockItem: function() {
            if (typeof this.blockUi === 'undefined') {
                this.blockUi = new Views.BlockUi();
            }
            this.blockUi.block(this.$el);
        },
        /**
         * unblock loading
         */
        unblockItem: function() {
            this.blockUi.unblock();
        }
    });
	 });
function doNowClick(){
	 console.log(window.AE.Models);
	 alert('Yes I am here');
}

要触发Model、Collection或View的主干事件,可以使用事件触发器:

object.trigger(event, [*args])

要触发常见的JS事件,可以使用jQuery触发器:

$('a.action').trigger('click');

或更简单的点击事件:

$('a.action').click();

最新更新