inwpitrust
inwpitrust

Reputation: 561

jQuery PubSub based MVC isn't firing custom events

I'm trying to build a PubSub based MVC (because reasons). If you look at this jsFiddle you'll see that none of the custom events are firing, and I don't know why...

document.addEventListener('DOMContentLoaded', function() {
    var o = $({})

    var model = {
        initialize: function() {
            o.on('data.request', this.getData.bind(this))
        },

        getData: function() {
            var data = 'new data'
            o.trigger('data.received', data)
        }
    }

    var view = {
        initialize: function() {
            this.view = $('#view')
            o.on('data.received', this.update.bind(this))
        },

        update: function(e, data) {
            this.view.html(data)
        }
    }

    var controller = {
        initialize: function() {
            this.bindEvents()
        },

        bindEvents: function() {
            o.on('click', '.js-data-request', this.request.bind(this))
        },

        request: function() {
            o.trigger('data.request')
        }
    }

    model.initialize()
    view.initialize()
    controller.initialize()
})

Could someone enlighten me?

Upvotes: 0

Views: 34

Answers (1)

topheman
topheman

Reputation: 7902

In your controller, you forgot to expose your events to the view (instead, you tie it to your pub/sub object). You should do something like :

bindEvents: function() {
    $('body').on('click', '.js-data-request', this.request.bind(this))
}

Upvotes: 1

Related Questions