-
Notifications
You must be signed in to change notification settings - Fork 9
View layer
Documentation of the view layer interface of Tetra.js
-
register([string] name, [object] implementation): register a new view class to tetra. -
destroy([string] name, [string] scope): destroy a previously registered view class. -
notify([string] message, [mixed] data, [string] scope): send an event with data to views in the specified scope. This method must be called in controller classes throughapp.notify. -
debug: debugging console. Specifications with the commandtetra.view.debug.man.
This method allows you to register a view description object.
-
name [string]: The name of the view that is coupled to module filename. It must be the same as js file name. For example: if you call your view likeButton, included file will be named “likeButton.ui.js”. -
implementation [object]: This object describes view module listeners and notifiers as well as DOM handlers. A more detailed definition follows.
{
scope : 'scope name', // #string
use : ['appCtrl', ...], // #array of controllers' names dependencies
root : 'containerId', // #string
constr : behaviors // #function
} -
scope [string]: it’s the visibility scope of message sent by the view to controller. Actually this is the application scope and it must be the application name. For example: for the “like” feature, the scope could be “likeApp”. -
use [array]: the list of controllers required by the view to work properly. This list is mandatory when the view is loaded using the bootstrap feature (see below). -
root [string]: [optional] id of a root element for the view. It will limit selectors matching to this container.
constr : function(me, app, _) { return {
events : {...},
methods : {...}
}; }constr is a function returning view’s behaviors in the following context :
-
me: this variable give you access to all data of the view object generated : scope, events, methods and all other attributes you can attach to. -
app: the communication pipe to all controllers in the scope. -
_: abstracted javascript library based on jQuery structure with following available functions:-
_(*): returns matching elements -
_(*).hasClass(className): http://api.jquery.com/hasClass/ -
_(*).addClass(className): http://api.jquery.com/addClass/ -
_(*).removeClass(className): http://api.jquery.com/removeClass/ -
_(*).attr(attrName, value[optional]): http://api.jquery.com/attr/ -
_(*).parents(selector): http://api.jquery.com/parents/ -
_(*).find(selector): http://api.jquery.com/find/ -
_(*).is(selector): http://api.jquery.com/is/ -
_(*).val(value[optional]): http://api.jquery.com/val/ -
_(*).html(markup[optional]): http://api.jquery.com/html/ -
_(*).serialize(inObject[optional]): if “inObject” is set to true, returns values of form inputs in a javascript object. Else, returns a query string . -
_(*).siblings(): http://api.jquery.com/siblings/ -
_(*).prev(): http://api.jquery.com/prev/ -
_(*).next(): http://api.jquery.com/next/ -
_(*).append(elem): http://api.jquery.com/append/ -
_(*).prepend(elem): http://api.jquery.com/prepend/ -
_(*).before(elem): http://api.jquery.com/before/ -
_(*).after(elem): http://api.jquery.com/after/ -
_(*).replaceWith(elem): http://api.jquery.com/replaceWith/ -
_(*).remove(): http://api.jquery.com/remove/ -
_(*).css(value[optional]): http://api.jquery.com/css/ -
_(*).height(): http://api.jquery.com/height/ -
_(*).width(): http://api.jquery.com/width/ -
_(*).offset(): http://api.jquery.com/offset/ -
_.ajax(url, options): must not be used. Call to server are managed in controllers using ORM. -
_.bind(eventName, callback): must not be used. View syntax already manage event listening. -
_.unbind(eventName, callback): must not be used. View syntax already manage event listening. -
_.extend(target, object): http://api.jquery.com/jQuery.extend/. -
_.inArray(value, array): http://api.jquery.com/jQuery.inArray/. -
_.toJSON(object): returns a JSON string of an object. -
_.parseJSON(string): http://api.jquery.com/jQuery.parseJSON/. -
_.trim(string): http://api.jquery.com/jQuery.trim/.
-
events : {
user : {
'eventName' : {
'selector' : function(event, element){...},
...
},
...
},
window : {
'eventName' : function(event){...},
...
},
controller : {
'message' : function(data){...},
...
}
}events is a description of callbacks executed as a consequence of controller or user demand :
-
user: list of events listened on the page. For each event (click, mouseover, …), we specify a list of selectors (CSS2.1 syntax) that must be reactive to this action and the function called on action. This function has access to the native event and the matched DOM element in context. e.preventDefault() is automatically call when an event match with one of the callback (except for checkbox/radio input element and keydown event). If you want to disable this behavior, add a “no-prevent” class on your element.
Special event management:
* mouseover and mouseout : these events are cleaned. That means they work as mouseenter and mouseleave and then an element don’t catch event from its children.
* clickout : you can define a clickout listener for a specific selector. This callback function will be called when the user click outside a any element matching this selector. This listener only works if a click listener with the same selector is defined in the same view.
-
window: list of window events listened on the page. A callback function is associated to each event you want to listen. -
controller: list of messages sent by controllers listened in the view with the function that must executed at the reception. Datas given by controller are accessible.
constr : function(me, app) { return {
events : {...},
methods : {
init : function(){...},
customMethod : function(param){...},
...
}
}; }Methods are common functions used locally in the view. They can be called using the me keyword. For example :
me.methods.customMethod(data); to call the function customMethod.
init is a reserved method executed when the view is registered. It can be used to define some variables accessible in the view. For example to define a DOM access, you can define a variable as an attribute attached to me :
me.target = document.getElementById('targetId');
tetra.view.register('likeButton', {
scope : 'likeApp',
use : ['likeCtrl'],
constr : function(me, app, _) { return {
events : {
user : {
'click' : {
'css selector1': function(e, elm)
{
var data = { ... };
app.notify('ask this', data);
},
[...]
'css selector2': function(e, elm)
{
var data = { ... };
app.notify('ask that', data);
}
}
},
controller : {
'do this' : function(data){
me.methods.doTheJob('this', data);
// DOM modifications
},
[...]
'do that' : function(data){
me.methods.doTheJob('that', data);
// DOM modifications
}
},
methods : {
init: function() {
me.myViewVar = 'smthg';
// Initialization of the view
},
[...]
doTheJob: function(type, params){
// Common function use in the view
}
}
};}
});