0

I want to implement MVC pattern in JavaScript, but I have a problem with Controller implementation.

MVC concepts are clear to me. I understand how MVC works. I don't think that events is appropriate way build a Controller. And there are a lot of dependences because of edition of the event in JavaScript. I would like to see some small example of implementation of MVC Controller in JavaScript.

p.s. I want to implement MVC pattern by using only JavaScript, but I don't want to use any MVC open-source frameworks.

p.s. I don't want you to do my homework, but I really don't understand how to imprelent Controller in JavaScript.

7
  • 1
    Aren't controllers a server concept? You could do it in Node.JS on a server and stay with Javascript, but controllers in a browser don't make much sense. Commented Jul 17, 2012 at 17:29
  • As you can see - here is she talking about Javascript. And I think it's about client Javascript. So it's not about server Commented Jul 17, 2012 at 17:31
  • 2
    Maybe you could look at how existing frameworks do it. Commented Jul 17, 2012 at 17:32
  • @Juhana - there Is more that 40 Javascript MVC frameworks, but the problem is that if you don't know how they works, you are not a professional programmer.. Commented Jul 17, 2012 at 17:49
  • @Juhana - I saw a lot of programmers who new a lot of frameworks, but when those frameworks was broken - they doesn't know what to do. I don't think that "knowledge of frameworks" === "knowledge of language" && "knowledge of frameworks" === "knowledge of patterns". Dose this make sense to you? Commented Jul 17, 2012 at 19:15

1 Answer 1

1

basicly MVC is based apon the Observer pattern (and controller is also)

How you can implement Observer inside of Javascript?

function Observer() {
    var subscribers = [];
    return {
        subscribe: function(eventName, object) {
            subscribers.push({ event: eventName, target: object });
        },
        unsubscribe: function(object) {
            var indexToDelete = subscribers.indexOf(object);
            subscribers.splice(indexToDelete, 1);
        },
        trigger: function(eventName, p1, p2, p3, p4, p5) {
            for (var i = 0; i < subscribers.lenght; i++) {
                if (subscribers[i].event == eventName) {
                    //target object must implement this function
                    subscribers[i].target[eventName](p1, p2, p3, p4, p5);
                }
            }
        }
    }
}

And how you can use it?

var model = { 
    fireChangesInsideOfModel: function(p1){ 
        //do some model update logic here
    } 
}
var controller = Observer();
    controller.subscribe("fireChangesInsideOfModel", model);

controller.trigger("fireChangesInsideOfModel", 11231); // 11231 - is an example of a parameter

Hope this helps you..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.