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(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. exWdgt.WidgetModel = can.Model({...}); exWdgt.WidgetControl = can.Control({...}); // Add an initialization function if the widget should be initialized externally exWdgt.initWidget = function(/*params*/){...}; // Make sure to add widget namespace to ns namespace ns.ExampleWidget = exWdgt; // Pass in external values that will be used by this code }( // ns should always be included, be first and be the specific namespace to which the resulting objects and functions from this file will be added. raft.widgets, // ns raft.data, // data raft.utils // utils )); $(document).ready(function()){ // Code to run when document is finished loading }
Utilities
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.