Questions about Controller and Class in JavaScriptMVC framework

205 Views Asked by At
$.Controller("Whiteboard", {}, {
    init: function(){
        var pen = new Pen();
    },
    sendRequest: function() {
        // This function should listen draw/erasing events from Pen
    }

});
$.Class("Pen",{},{

    init: function() {
        // Pen setup, click to draw etc..
    }
});

Something like this, I want the Whiteboard listen events from Pen, how can I do this? I need Controller listen on a Class.

1

There are 1 best solutions below

1
On

If this is JMVC 3.2+ you can do something like this:

$('.whiteboard').whiteboard({pen:new Pen()});

$.Controller("Whiteboard", {}, {
    "{pen} draw": function() {
        this.sendRequest();
    }
});

$.Observe('Pen',{},{
    draw: function() {
        $(this).triggerHandler('draw');
    }
});

For JMVC 3.1, you'll have to use $.Model instead of $.Observe and use this.bind in your controller like this:

$.Controller("Whiteboard", {}, {
    init: function() {
        this.bind(this.options.pen,'draw',this.callback('sendRequest'));
    }
});