10

I created a small sudoku app using Javascript. Now I am trying to convert that javascript code into extjs (4.1.1a) code. I have gone through the docs to understand the MVC Architecture, but it seemed not so detailed for me as I am a beginner.

Can someone please explain the MVC Architecture of Extjs based on my Sudoku app?

The design of my sudoku app code is as follows:

enter image description here

The description of the above design is as follows:

  • container (blue) --> parent panel (grey) --> child panel (red)

  • The "parent panels" are nine and each "parent panel" has nine "child panels".

  • The HTML elements of "parent panels" and the "child panels" are being generated dynamically by using for loops.

  • I have written events like KeyDown events and click events on "child panels".

  • I have also written some functions like

    checkGroup()       --> checks in each "parent panel" whether there are any duplicate numbers checkVertical()     --> checks in each vertical line of "container" for duplicate numbers checkHorizontal() --> checks in each horizontal line of "container" for duplicate numbers


EDIT: (unfinished and unstructured code)

app.js (main js file)

Ext.application({
     name: 'Game',
     appFolder: 'app',  
     controllers: ['Sudoku']     
});

controller ('app' folder --> 'controller' folder --> Sudoku.js)

//By using 'controller', trying to call 'view' here
Ext.define('Game.controller.Sudoku', {
    extend: 'Ext.app.Controller',

    init: function () {
        console.log("controller init");
    },
    onLaunch: function () {
        console.log("controller onLaunch");
    },
    views: ['Sudoku']
});

view ('app' folder --> 'view' folder --> Sudoku.js)

Ext.define('Game.view.Sudoku', {
    extend: 'Ext.window.Window',  //what should I extend here for view?       
    initComponent: function () {
        //my complete sudoku js file here
        console.log("hello");
    }
});
Community
  • 1
  • 1
Mr_Green
  • 36,985
  • 43
  • 143
  • 241
  • You require a sudoku controller but have named it users... – sra Feb 09 '13 at 15:30
  • @sra sorry it was typo, I changed it. eventhough I can't see "hello" in my chrome console. I extended controller and view as mentioned in the docs. Am I doing here correct? – Mr_Green Feb 10 '13 at 03:06
  • @sra somehow I get into the controller part. (showing the messages of controller). Now it is just not showing 'view' message i.e., 'hello'. where could I have gone wrong I can't understand. – Mr_Green Feb 10 '13 at 04:23
  • You need to tell the controller what he should do. For example loading your view when he loads. For that you may use refs (there are some other ways but I think refs are the easiest in your case) note to define the refs before you call them. – sra Feb 10 '13 at 06:29
  • @sra Can you please show me where to give ref (ref seems to be private)? I even asked the same question on [sencha forums](http://www.sencha.com/forum/showthread.php?256080-MVC-architecture-of-extjs&p=937392#post937392) – Mr_Green Feb 10 '13 at 06:47
  • Please read [here](http://docs.sencha.com/ext-js/4-1/#!/api/Ext.app.Controller-cfg-refs) about refs and note the plural ;) there are a some examples concerning refs if you scroll up. – sra Feb 10 '13 at 06:54

2 Answers2

8

From all that I know of your app I can say nearly nothing. You have a really specific view with some listeners and actions where none should bother a controller.

A controller would create the container as view and may pass some config options to it without bothering much about the other nested panels. The controller may also listen to events of this container like a button that ends the game or save the game.

MVC doesn't mean that you would relay all events and logic into the controller.

Even if this is in your opinion rather complex it is still just a view.

sra
  • 23,629
  • 7
  • 52
  • 88
  • Can you please explain me what does model do? (just for knowledge) – Mr_Green Feb 08 '13 at 12:52
  • 1
    @Mr_Green Well, the approach that ExtJS took is nice but it is not actually what can take to understand the MVC pattern. Please read this [Article on Wikipedia](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) or this from [Article from Microsoft](http://msdn.microsoft.com/en-us/library/ff649643.aspx) to understand that pattern more in deep – sra Feb 08 '13 at 14:12
  • please have a look at my post edit. I added how I did it.. The code is not even showing any kind of errors. where could I have gone wrong in calling the functions? – Mr_Green Feb 09 '13 at 14:55
  • Hi, I solved it by giving alias to view and calling that alias name in controller. :) – Mr_Green Feb 11 '13 at 08:23
  • 1
    @Mr_Green That is one way (btw. you should always define a alias!). Refs can help you manage access & instances of views like autocreate, singleton, ... It's a wrapper that can save you lines of code while it encapsulates these parts for you. – sra Feb 11 '13 at 08:28
6

First, you should have a good understanding of how MVC works before attempting to implement it, especially in Ext JS which had MVC support tacked on in a recent version.

Speaking in the general sense (since you're the only one who really knows your code), I would separate the code as such:

  • Model: A 9x9 matrix of data values (or a 3x3 matrix of 3x3 matrices), a validation method that determines if the puzzle is solved or if there are any errors in the user input (eg. two 6's in a box), and possibly undo support.

  • View: Your container above. The controller should have no idea how the container displays values. I'd probably send my own sudoku-specific events like cellchanged(container, x, y, newValue, oldValue) and undo(container).

  • Controller: Listens for the sudoku-specific events in the view and updates the model accordingly. After each update, validates the model to see if the puzzle has been solved or if certain cells are incorrect. Should not act as a relay for all view events. Events like render and resize aren't relevant to the sudoku controller. Only listen for what you actually need.

Eric
  • 6,775
  • 24
  • 32
  • Nice explanation. But Model is bound to the view as well. Relay all over the controller is against the separation. But all in all +1 – sra Feb 08 '13 at 17:23
  • I've never liked having the Model talk to the View. I suppose my explanation is more like Presentation Abstraction Control than true MVC. But there's a point where you have to stop worrying about architecture semantics and just code the damn thing. +1 back at ya. – Eric Feb 08 '13 at 18:36
  • please have a look at my post edit. I added how I did it.. The code is not even showing any kind of errors. where could I have gone wrong in calling the functions? – Mr_Green Feb 09 '13 at 14:56