lundi 26 janvier 2015

Where should events ('click','change') come in on MVC in javascript?

I'm a bit lost with implementing MCV pattern on javascript.


Where should events (such as 'click','change') come in on MVC in javascript?


I have this event function,



var EventExample = function (controller, view) {

this.controller = controller;
this.view = view;
return this;
};

EventExample.prototype.clicked = function ( ) {

var $root = this;

var id = $("#input-text").val();

$root.controller.find( id );

return $root.view.render();
};


And I have this function to bind all MVC, Mapper, Service, etc together,



function bootstrapper() {

// get a new model.
var model = new ModelExample();

// get a new model.
var mapper = new MapperExample();

// get a new service.
var service = new ServiceExample( mapper, model);

// get a new controller.
var controller = new ControllerExample(service);

// get a new view.
var view = new ViewExample(model);

// get a new event.
var event = new EventExample(controller, view);

$('#search-button').click(function(){
event.clicked();
});
}


That is how I call the event function in there, but it seems weird.


I looked at this blog, the event (click) seems must be wrapped inside a function? if so, is it supposed to be in a controller, or in a model, or in a mapper?


Are all events considered Observers in the design patterns?


Any ideas how I should go with events?


My entire MVC code is on jsfiffle


Aucun commentaire:

Enregistrer un commentaire