core.js | |
---|---|
/*global Ply, jQuery */
/*jshint eqeqeq: true, curly: true, white: true */ | |
Ply is a lightweight JavaScript framework for creating reusable UI components and managing application logic. It aims to eliminate boilerplate code, create a consistent interface, and provide common patterns for code organization and re-use. | |
Ply is not an MVC framework and doesn't aim to compete with Backbone.js or SproutCore. Instead, it is intended for users who need to maintain logic on the server and are looking for a better option for code re-use and organization. | |
This file comprises the Core module. | |
Declare global namespace and assign version number. | window.Ply = {
VERSION: '0.2.3'
}; |
Define | Ply.core = (function ($) { |
Private VariablesCreate private variables. | var listeners = {},
debug = false; |
Public Methods/Properties | return { |
NotifyNotifies listeners of an event. Notifiers should send themselves and optional data as arguments. | notify: function (note, sender, data) { |
Cache listeners array or create a new array, assign, and cache it. | var list = listeners[note] || (listeners[note] = []), |
Create loop variables. | i = 0,
len = list.length; |
Loop over listeners and notify each. | for (; i < len; i++) {
list[i].handler.call(list[i].listener, note, sender, data);
}
}, |
ListenListens for a particular notification or set of notifications.
Clients should pass in a handler function and themselves as arguments.
When the handler function is called, it will be applied to the | listen: function (notification, handler, listener) { |
Cache the notification's listeners if it exists or create and cache a new array otherwise. | var list = listeners[notification] || (listeners[notification] = []), |
Split the notification on whitespace. Clients can listen to multiple notifications by passing in a string with the notification names split by whitespace. | notes = notification.split(/\s/), |
Create loop variables. | len = notes.length,
i = 0; |
If the notification name contains whitespace, listen on each particular notification (segment). | if (len > 1) {
for (; i < len; i++) {
this.listen(notes[i], handler, listener);
}
return;
} |
Add the listener and handler function to the notifications array. | list.push({
handler: handler,
listener: listener
});
}, |
LogLightweight logging wrapper around | log: function (msg, type) { |
Do nothing if debug mode is disabled. | if (!debug) {
return;
} |
Use the correct logging mechanism based on the parameter type. | if (window.console) {
switch (type) {
case 'warn':
console.warn(msg);
break;
case 'info':
console.info(msg);
break;
default:
console.log(msg);
break;
}
}
}, |
ErrorMethod to catch JavaScript errors. All Ply methods are run in a | error: function (ex, sev) { |
If an | if (Ply.config.core.onError && typeof Ply.config.core.onError === 'function') { |
Pass in the exception and the severity to the handler. | Ply.config.core.onError(ex, sev);
return;
} |
If no error handler is defined, simply throw the error. | throw ex;
}, |
DebugEnabled debugging when called with no argues or with any truthy value. To disable debug mode
send | debug: function (val) { |
Default to | debug = val || true;
if (debug) { |
Print debug notice. | this.log('debugging...', 'info'); |
Manually set | this.debugOn = true;
}
else { |
Do the opposite. | this.debugOn = false;
}
}, |
Debug onCache the value of debug. This is used by clients to determine if debug mode is currently enabled. | debugOn: function () { |
Coerce the value to a boolean. | return !!debug;
}()
}; |
Alias | })(jQuery); |
↪ Ajax | |