petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

Maria not using the browser console for debugging. #72

Open ryan-scott-dev opened 10 years ago

ryan-scott-dev commented 10 years ago

It was instead trying to reference an undefined console variable scoped within the maria IIFE.

petermichaux commented 10 years ago

This is a good find.

Using window may not be the best option for getting the global object because someone might be using Maria in a non-browser environment.

ryan-scott-dev commented 10 years ago

So I guess the best option would be to fetch console before the IIFE.

I'm thinking something along the lines of:

var globalConsole = console;
var maria = (function() { // IIFE

And then in namespace.js

var maria = {};

/* DEBUG BEGIN */
// Help older browsers without the `console` host object.
var console = globalConsole || {};
console.log = console.log || function() {};
console.warn = console.warn || function() {};
console.error = console.error || function() {};
/* DEBUG END */

What are your thoughts?

petermichaux commented 10 years ago

The point of the IFFE is so that only one global is defined. I'd like to stay with that purity as this is important to many people.

I think the following would work.

var maria = (function(/* DEBUG BEGIN */console/* DEBUG END */) {
    /* DEBUG BEGIN */
    console = console || {};
    //...
    /* DEBUG END */ 

    //...

}(/* DEBUG BEGIN */(typeof console === 'object') ? console : null/* DEBUG END */));
petermichaux commented 10 years ago

@Archytaus, any more thoughts on this?

ryan-scott-dev commented 10 years ago

It seems to work fine for me. I've updated the PR. I'm not sure how to apply this to AMD though, if it is an issue there as well.