h2. Standards (proposed)
h4. Code Files
Should put all code, other than $(document).ready() blocks, within self-executing closures with important namespaces passed in:
{code}
// Example Widget
function(app, ns, data, utils){
// Create Widget short name for local reference exWdgt = {};
// Use closures for private code var testFunc();
// Add all widget code to widget namespace.
ns.WidgetModel = can.Model({...});
ns.WidgetControl = can.Control({...});
// Add an initialization function if the widget should be initialized externally
ns.initWidget = function(/*params*/){...};
// Pass in external values that will be used by this code
}(
raft.app, // The app namespace
raft.widgets.ExWidget, // The specific namespace to which the resulting objects and functions from this file will be added.
raft.data.ExWidget, // The specific data namespace for this widget/page/object. Cached and local data should go here
raft.utils // The namespace containing app framework utilities
));
$(document).ready(function()){
// Code to run when document is finished loading
}
{code}
h5. Standard Page files and Naming
*pages/PageName/PageNameDef.js:* Creates namespace for page and data and optionally dynamically loads files required.
*pages/*{*}Page{*}{*}Name/*{*}Page{*}{*}Name.css:* Styles specific to this page
*pages{*}*/*{*}Page{*}{*}Name/*{*}Page{*}{*}NameModel.js:* Models used by this page
*pages{*}*/*{*}Page{*}{*}Name/*{*}Page{*}{*}NameCtrl.js:* Controllers used by this page
*pages{*}*/*{*}Page{*}{*}Name/*{*}Page{*}{*}NameView.ejs:* Main page view template
*pages{*}*/*{*}Page{*}{*}Name/*{*}Page{*}{*}NameViews.ejs:* Optional second/third/... view templates
h5. Standard Widget files and Naming
*widgets/WidgetName/WidgetNameDef.js:* Creates namespace for widget and data and optionally dynamically loads files required.
*widgets/WidgetName/WidgetName.css:* Styles specific to this widget
*widgets/WidgetName/WidgetNameModel.js:* Models used by this widget
*widgets/WidgetName/WidgetNameCtrl.js:* Controllers used by this widget
*widgets/WidgetName/WidgetNameView.ejs:* Main widget view template
*widgets/WidgetName/WidgetNameViews.ejs:* Optional second/third/... view templates
h5. Sample Widget/Page Definition File
{code}
// Example Widget Definition
function(app, ns, data, utils){
// Create Widget short name for local reference
ns.ExampleWidget = {
SomeWidgetFunctionObjectOrEnum : {...}
};
// Create Widget data namespace
data.ExampleWidget = {
WidgetDataVal1 : "foo",
WidgetDataVal2 : {...}
};
// It's common to always defer loading of css and data since they aren't debugged
utils.requireCSS("widgets/ExampleWidget/ExampleWidget.CSS");
utils.requireScript("widgets/ExampleWidget/widget_test_data.js", false);
// Only defer load these if it's turned on
if (app.config.isDeferLoad) {
utils.requireScript("widgets/ExampleWidget/ExampleWidgetModel.js", true);
utils.requireScript("widgets/ExampleWidget/ExampleWidgetCtrl.js", true);
}
// Pass in external values that will be used by this code
}(
raft.app, // The app namespace
raft.widgets, // The widget or page namespace
raft.data, // The data namespace
raft.utils // Application framework utilities namespace
));
$(document).ready(function()){
// Code to run when document is finished loading
}
{code}
h2. Documentation
PhpStorm has a shortcut for creating object and function documentation. Just place the cursor on the line just prior to the function/object you want documented and type /*\* then hit enter. It will create a documentation block matching in jsDoc/javaDoc syntax. Then just add your documentation to the block. It even uses introspection to do it’s best to determine the types of the params and return value.
{code}
1
2 function foo(param1, param2) {
3 return param1 + param2
4 }
While in PhpStorm place the cursor on line 1 and type /** and hit enter. It will generate the following block:
1 /**
2 *
3 * @param param1
4 * @param param2
5 * @return *
6 */
7 function foo(param1, param2) {
8 return param1 + param2
9 }
{code}
h2. Utilities
h5. Helper Functions
{code}
$.isDefined(obj) // Returns true if object is not undefined and not null
String.isString(obj) // Returns true if object is a string literal or String object
String.isEmpty(obj) // Returns true if object is a zero-length string
String.isNonEmpty(obj) // Returns true if object is a non-zero-length string
Number.isNumber(obj) // Returns true if object is a primitive number or a Number object
{code}
h5. Dynamic Loader Functions
requireScript(): Loads the specified script, synchronously by default. If the file has already been loaded it will not be loaded again and a comment will be logged to that effect. The return value is a promise.
{code}
raft.utils.requireScript(path).always(...).then(...);
{code}
requireCSS(): Adds a tag to the document.head element which loads the specified CSS file.
{code}
raft.utils.requiresCSS(path);
{code}
h2. Misc:
\- Always include both params when calling can.Model() |