Standards (proposed)
Code Files
Should put all code, other than $(document).ready() blocks, within self-executing closures with important namespaces passed in:
// 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 }
Utilities
Helper Functions
$.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
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.
raft.utils.requireScript(path).always(...).then(...);
requireCSS(): Adds a tag to the document.head element which loads the specified CSS file.
raft.utils.requiresCSS(path);
Misc:
- Always include both params when calling can.Model()
- Discovered that calling can.route.link() returns an escaped string. This means that using it from within a view to create an <a href> does not work and instead adds "<a href>" to the page. The only way to do this that I have discovered so far is to use the can.route.link() call from within an append() call in the controller after the view finishes rendering. There may be other ways to do this but I haven't looked for them.