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

时间:2016-01-04 08:05:34

标签: backbone.js

如何在代码中描述的使用自定义函数调用骨干事件?我使用自己的名为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');
}

1 个答案:

答案 0 :(得分:-1)

要触发模型,集合或视图的主干事件,您可以使用event trigger

object.trigger(event, [*args])

触发可以使用jQuery trigger的常见JS事件:

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

或点击事件更简单:

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