Separation of Concerns in controllers and views

74 views
Skip to first unread message

Al

unread,
May 9, 2014, 9:11:44 AM5/9/14
to tot...@googlegroups.com
I need to separate controllers into groups, so that each group of controllers operate as a unit.

/controllers
   admin.dashboard.js
   admin.articles.js
   admin.categories.js
/views:
    _adminlayout.html
    /admin
          dashboard.html
          articles.html
          categories.html
          /partials
             modal-form-article.html
             modal-form-categories.html

Reason: when submitting an article if user needs to add a new category. I would like to call "modal.form.categories" for quick access.

Problem: 
1) At the moment I am using only one controller "admin.js". 
2) @{model.blahh} or @{repository.blahh} or @{options(repository.blahh, model.blahh, 'name', 'id'}  all fail at the /partial level as undefined.

How can I split this into multiple controllers and maintain all in relation to the view "/admin"?
Thanks.
             

Peter Širka

unread,
May 9, 2014, 1:41:07 PM5/9/14
to tot...@googlegroups.com
Hi AI,
Can you create small example?
I understand to only a part and example describes for me more.

Thanks :-)

Al

unread,
May 10, 2014, 2:37:01 PM5/10/14
to tot...@googlegroups.com
I have attached the issue in first part "Splitting Controller". I will send @{} in partials next.
soc.zip

Peter Širka

unread,
May 10, 2014, 5:48:39 PM5/10/14
to tot...@googlegroups.com
Ahh yes...

1.
total.js supports directories in controllers, example:

/controllers/admin/arcticles.js  --->  /views/admin/articles/some-view.html
/controllers/admin/categories.js  --->  /views/admin/categories/some-view.html

2. 
create a new definition: views.js

framework.on('controller', function(controller, name) {
   
// set default directory for views
   
if (name.indexOf('admin.') !== -1)
        controller
.currentView('/admin/');
});

3.
add ~ (read view from a view root path)
// controllers/articles.js
controller
.view('~/admin/articles'); // ---> /views/admin/articles.html

// controllers/categories.js
controller
.view('~/admin/categories'); // ---> /views/admin/categories.html



Thanks :-)

Al

unread,
May 10, 2014, 10:04:29 PM5/10/14
to tot...@googlegroups.com
Thanks.


On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Al

unread,
May 11, 2014, 4:04:07 PM5/11/14
to tot...@googlegroups.com
Another question with example. How could I separate the route I get 'apply' not defined'.



On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:
soc2.rar

Al

unread,
May 12, 2014, 11:29:31 AM5/12/14
to tot...@googlegroups.com
Thank you, Peter, I will add this to the docs shortly....

On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Al

unread,
May 13, 2014, 9:50:21 PM5/13/14
to tot...@googlegroups.com
Hello again, I a having one additional issue using "controller-sharing". I have a attached an example. 

Thanks...


On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:
controller-sharing-advanced.zip

Al

unread,
May 14, 2014, 12:56:26 AM5/14/14
to tot...@googlegroups.com


Resolved....................................



On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Al

unread,
May 14, 2014, 1:02:37 AM5/14/14
to tot...@googlegroups.com
var get =  require('../dir-name/users');

exports.default = function view_homepage() {
var self = this;
var users = get.functions;

    console.log(users);

var builder = [];

builder.push('Exists: Peter = {0}'.format(users.exists('Peter')));
builder.push('Exists: Jolaus = {0}'.format(users.exists('Jolaus')));
builder.push('Exists: Lucia = {0}'.format(users.exists('Lucia')));
builder.push('');
builder.push('All users: {0}users/'.format(utils.path(self.req.uri.href)));

self.plain(builder.join('\n'));
}




On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Al

unread,
May 14, 2014, 1:09:26 AM5/14/14
to tot...@googlegroups.com
Just for reference.

On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:
controller-sharing-advanced.zip

Peter Širka

unread,
May 14, 2014, 4:26:08 AM5/14/14
to tot...@googlegroups.com
For this case use a source directory (business layer) - http://docs.totaljs.com/global/#SOURCE.
Other directories in the project is bad practice.

Thanks.

Al

unread,
May 14, 2014, 10:18:39 AM5/14/14
to tot...@googlegroups.com
I am using data from another controller, I need to share data between controllers in the same dir. Are you saying require the controller under SOURCE and require this in new controller. Please see the attached example.



On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:
controller-sharing-advanced.zip

Peter Širka

unread,
May 15, 2014, 3:37:04 AM5/15/14
to tot...@googlegroups.com
I think that your example is not good solution.

Why?

Controllers can have some actions. My idea: action will must be simple and other components will be separated into the modules. This is simple case for testing and maintance. And for other we must use a SOURCE - app business layer.

You want to add some business logic into the controllers and this is a bad practice.
I'll create example.

Al

unread,
May 15, 2014, 2:18:18 PM5/15/14
to tot...@googlegroups.com
Thanks as is it not clear how it should be approached.


On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Al

unread,
May 18, 2014, 9:53:56 AM5/18/14
to tot...@googlegroups.com
Hello Peter, Just following up on this. As I would really appreciate the example.


On Friday, May 9, 2014 9:11:44 AM UTC-4, Al wrote:

Peter Širka

unread,
May 18, 2014, 1:20:39 PM5/18/14
to tot...@googlegroups.com
Hi AI,
this is a good point where to start:

Currently I haven't any free time ... but I'm thinking about it.
Thanks for understanding.

Reply all
Reply to author
Forward
0 new messages