Hey Friends,
I am back with part-2 of my post for MVC Application in XQuerrail framework.
Just for a quick recall, previously on my post - MVC Application in XQuerrail Framework - Part - 1, we discussed about how we can create an entity/screen with basic CRUD operations without any code for controller or views.
So today we will extend our application as developed in previous post to include simple customization of view, controller and model for specific requirement to demonstrate custom model, view and controller implementation in XQuerrail framework based MVC application.
As we already know that XQuerrail framework is an stand alone folder based framework and strict follow of folder structure is needed in XQuerrail framework base MVC application. So Let’s start with folder structure of XQuerrail framework based application for MVC.
As we discussed previously in XQuerrail Framework Based Application With Mark Logic that “main” folder is mainly containing all required code to run XQuerrail framework based applications and app” folder in “main” contains entire application specific code for an application
Below are the folders can be created in “app” folder if custom implementation of model, view or controller is required.
“/app/controller/” - All custom controller of application for any model must be placed in this folder with the naming convention as <controller_name_as_configured_in_application-domain.xml>-controller.xqy.
For ex. If we need to create custom controller for “person” then we should create a file in “/app/controller/” folder with name as “persons-controller.xqy” where “persons” is controller name as we defined in application-domain.xml for “person” model. (refer part 1 of this article)
“/app/models/” - All custom model of application, to communicate with database must be kept in this folder which are used to implement custom methods as not provided by framework by default or if we need to override definition of existing method of framework for specific model. Name of .xqy file in this folder should be exactly same as model name as specified in application-domain.xml.
“/app/views/” - All custom views specific to specific controller must be placed in this folder with in a folder named as controller name for model and file name should be as <controller_name_as_configured_in_application-domain.xml>.<view_name_to_customize>.html.xqy
For ex - If we want to have custom view (UI) for adding new “person” model record which is related to “persons” controller than “persons.new.html.xqy” should be kept in “/app/views/persons/” folder.
You can get base implementation of “new” view which is used by framework to create record of specific model from views folder in framework i.e. “/main/_framework/base/views/”. Copy “base.new.html.xqy” file from this folder to your custom view folder and rename this file as discussed above and make required customization in code.
That is enough for theory now lets create an example using model, view and controller.
Consider following scenario to achieve. We need to create screen where we need to create new record belonging to a role model. But we need additional information belongs to other model i.e. Pages which is saved in database. On the screen of create new role screen we need to display a list of all pages available for selection which should be assigned to specific role.
Now consider below definition for role
<model name="role" persistence="document" key="uuid" keyLabel="name" extends="base" >
This will create by default name field only. Now we need additional page list on UI for while adding new role. If we need list of all pages in another XML document then we can proceed following steps to achieve above considered example.
Step1 :- Create a model in models folder for role and name as “role.xqy”
In this step we are adding custom model for role which can be utilize to get additional information or perform additional operation which are not performed by default model as created by framework. So here we can write method that interacts with database to get pages list.
Step2 :- Create a controller in controllers folder for role and name as “roles-controller.xqy”
Implement code to handle create new role request here so that it will provide additional pages list to render on uI while creating new role. Below is code snippet to give an idea to override new() request to get additional information of page list.
declare function controller:get-pages()
{
(: Function created in custom model too get page list from database :)
model:get-pages()
};
declare function controller:new()
{
let $body :=
element role {
attribute {"xsi:type"} {"role"},
attribute {"xmlns"} {"http://xquerrail.com/app"},
attribute {"xmlns:app"} {"http://xquerrail.com/app"},
element name {""},
element pages {
controller:get-pages()/page
}
}
return
(
response:set-template("main"),
response:set-title(controller:model()/@label),
response:set-view("new"),
response:set-body($body),
response:flush()
)};
Step3 :- Create a custom view to create new role in views folder and name as “roles.new.html.xqy”
Create or modify html of new role creation screen to include page list to display on UI as will be provided from controller through overridden new() request. Below is code part for modified new.html.xqy
<div class="span8">
<b>Name</b><br/>
<input type="text" name="name" ></input>
<br/>
<br/>
<b>Pages</b>
<ul>
{
for $itm in response:body()//*:page
return
<li><input type="checkbox" >{$itm}</input></li>
}
</ul>
</div>
So friends, hopefully you will be having a good idea about model, view, controller for XQuerrail framework and their linking to work together. But as i believe the best way is to exercise yourself. So, try to implement above explained example yourself and then move further for deep dive in.
Till the time keep exploring.
I am back with part-2 of my post for MVC Application in XQuerrail framework.
Just for a quick recall, previously on my post - MVC Application in XQuerrail Framework - Part - 1, we discussed about how we can create an entity/screen with basic CRUD operations without any code for controller or views.
So today we will extend our application as developed in previous post to include simple customization of view, controller and model for specific requirement to demonstrate custom model, view and controller implementation in XQuerrail framework based MVC application.
As we already know that XQuerrail framework is an stand alone folder based framework and strict follow of folder structure is needed in XQuerrail framework base MVC application. So Let’s start with folder structure of XQuerrail framework based application for MVC.
As we discussed previously in XQuerrail Framework Based Application With Mark Logic that “main” folder is mainly containing all required code to run XQuerrail framework based applications and app” folder in “main” contains entire application specific code for an application
Below are the folders can be created in “app” folder if custom implementation of model, view or controller is required.
“/app/controller/” - All custom controller of application for any model must be placed in this folder with the naming convention as <controller_name_as_configured_in_application-domain.xml>-controller.xqy.
For ex. If we need to create custom controller for “person” then we should create a file in “/app/controller/” folder with name as “persons-controller.xqy” where “persons” is controller name as we defined in application-domain.xml for “person” model. (refer part 1 of this article)
“/app/models/” - All custom model of application, to communicate with database must be kept in this folder which are used to implement custom methods as not provided by framework by default or if we need to override definition of existing method of framework for specific model. Name of .xqy file in this folder should be exactly same as model name as specified in application-domain.xml.
“/app/views/” - All custom views specific to specific controller must be placed in this folder with in a folder named as controller name for model and file name should be as <controller_name_as_configured_in_application-domain.xml>.<view_name_to_customize>.html.xqy
For ex - If we want to have custom view (UI) for adding new “person” model record which is related to “persons” controller than “persons.new.html.xqy” should be kept in “/app/views/persons/” folder.
You can get base implementation of “new” view which is used by framework to create record of specific model from views folder in framework i.e. “/main/_framework/base/views/”. Copy “base.new.html.xqy” file from this folder to your custom view folder and rename this file as discussed above and make required customization in code.
That is enough for theory now lets create an example using model, view and controller.
Consider following scenario to achieve. We need to create screen where we need to create new record belonging to a role model. But we need additional information belongs to other model i.e. Pages which is saved in database. On the screen of create new role screen we need to display a list of all pages available for selection which should be assigned to specific role.
Now consider below definition for role
<model name="role" persistence="document" key="uuid" keyLabel="name" extends="base" >
<document root="roles">/lookup/roles.xml</document>
<navigation newable="true" listable="true" editable="true" removable="true" />
<element name="name" type="string" label="Name" />
</model>
This will create by default name field only. Now we need additional page list on UI for while adding new role. If we need list of all pages in another XML document then we can proceed following steps to achieve above considered example.
Step1 :- Create a model in models folder for role and name as “role.xqy”
In this step we are adding custom model for role which can be utilize to get additional information or perform additional operation which are not performed by default model as created by framework. So here we can write method that interacts with database to get pages list.
Step2 :- Create a controller in controllers folder for role and name as “roles-controller.xqy”
Implement code to handle create new role request here so that it will provide additional pages list to render on uI while creating new role. Below is code snippet to give an idea to override new() request to get additional information of page list.
declare function controller:get-pages()
{
(: Function created in custom model too get page list from database :)
model:get-pages()
};
declare function controller:new()
{
let $body :=
element role {
attribute {"xsi:type"} {"role"},
attribute {"xmlns"} {"http://xquerrail.com/app"},
attribute {"xmlns:app"} {"http://xquerrail.com/app"},
element name {""},
element pages {
controller:get-pages()/page
}
}
return
(
response:set-template("main"),
response:set-title(controller:model()/@label),
response:set-view("new"),
response:set-body($body),
response:flush()
)};
Step3 :- Create a custom view to create new role in views folder and name as “roles.new.html.xqy”
Create or modify html of new role creation screen to include page list to display on UI as will be provided from controller through overridden new() request. Below is code part for modified new.html.xqy
<div class="span8">
<b>Name</b><br/>
<input type="text" name="name" ></input>
<br/>
<br/>
<b>Pages</b>
<ul>
{
for $itm in response:body()//*:page
return
<li><input type="checkbox" >{$itm}</input></li>
}
</ul>
</div>
So friends, hopefully you will be having a good idea about model, view, controller for XQuerrail framework and their linking to work together. But as i believe the best way is to exercise yourself. So, try to implement above explained example yourself and then move further for deep dive in.
Till the time keep exploring.
No comments:
Post a Comment