On Jun 12, 5:03 pm, Gordon wrote:
The eval was a quick and dirty alternative to a switch
statement for each kind of object that i want to be able
to instantize. The problem is that using the eval is
screwing up the scoping rules that normally apply in
javascript that involve coming up with inelegant workarounds,
The scope used in an eval execution context is the same scope as the
execution context from which eval is called, so what you are saying
does not make sense.
such as putting stuff that I'd normally keep in the scope
of the cose I'm running in the global scope instead.
And that makes even less sense.
Of the two choices the switch statement is probably the better
one, but it adds an extra point in the code that has to be
changed should, for example, I choose to add a new object type.
It also adds n lines of code per object type, and I'd like to
find a better solution. I guess what I really want to be able
to do is something like:
var localVar = new objType.value ()
But when I try that I just get an objType.value is not a
function message.
As is should, but if you provided some context for you code you might
get a decent answer to your question. Which will be how a bracket
notation property access can be employed here. For example, if all
your constructors were globally declared functions and environment was
a web browser were no alternative means of accessing the global object
had been created those constructors could be references as members of
the object referred to by - window -. I.E.:-
var localVar = new window[objType.value]();
Here's the complete code. Bear in mind it's in a very early stage of
development, is very rough and ready, and has lots of code in the
middle of being rewritten
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "
http://www.w3.org/
TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="templates/interface/
styles/default.css" />
<!--[if ie 6]>
<link rel="stylesheet" type="text/css" href="templates/interface/
styles/fix_ie.css" />
<![endif]-->
<style type="text/css">
<!--
..fields {
border: solid 1px blue;
}
legend {
display: none;
}
..questionGrid {
width: 100%;
}
..cmsDialog {
display: none;
}
..cmsDialog ul {
list-style: none outside;
padding: 0;
margin: 0;
}
..cmsDialog label {
display: -moz-inline-box; /* For gecko */
display: inline-block; /* For Internet Explorer */
width: 75px;
}
-->
</style>
<link rel="stylesheet" href="/js/jquery/ui/themes/custom/theme.css"
type="text/css" media="screen" />
<script type="text/javascript" src="/js/jquery/jquery.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.livequery.js"></
script>
<script type="text/javascript" src="/js/jquery/ui/ui.core.js"></
script>
<script type="text/javascript" src="/js/jquery/ui/ui.draggable.js"></
script>
<script type="text/javascript" src="/js/jquery/ui/ui.resizable.js"></
script>
<script type="text/javascript" src="/js/jquery/ui/ui.sortable.js"></
script>
<script type="text/javascript" src="/js/jquery/ui/ui.dialog.js"></
script>
<script type="text/javascript">
<!--
var dialogs = {};
/*
function QuestionRadio ()
{
}
function QuestionSelect ()
{
}
function QuestionGridRow ()
{
}
*/
function QuestionString (target, label)
{
var self = this;
self.markup = $('<li><label class="sortHandle"><input type="hidden"
name="groups['
+ target.title + ']['
+ label
+ '][type]" value="string" />'
+ label
+ '</label><input type="text" name="groups['
+ target.title + ']['
+ label
+ '][default]" />'
+ '<input type="button" class="delQ" value="Delete question"></
li>');
self.construct = function ()
{
target.questionList.append (self.markup);
target.questionList.sortable ('refresh');
};
self.construct ();
}
function QuestionText (target, label)
{
var self = this;
self.markup = $('<li><label class="sortHandle"><input type="hidden"
name="groups['
+ target.title + ']['
+ label
+ '][type]" value="text" />'
+ label
+ '</label><textarea name="groups['
+ target.title + ']['
+ label
+ '][default]"></textarea>'
+ '<input type="button" class="delQ" value="Delete question"></
li>');
self.construct = function ()
{
target.questionList.append (self.markup);
target.questionList.sortable ('refresh');
};
self.construct ();
}
function QuestionCheck (target, label)
{
var self = this;
self.markup = $('<li><label class="sortHandle"><input type="hidden"
name="groups['
+ target.title + ']['
+ label
+ '][type]" value="check" />'
+ label
+ '</label><input type="checkbox" name="groups['
+ target.title + ']['
+ label
+ '][checked]" value="1" />'
+ '<input type="button" class="delQ" value="Delete question"></
li>');
self.construct = function ()
{
target.questionList.append (self.markup);
target.questionList.sortable ('refresh');
};
self.construct ();
}
/*
function QuestionGrid (target)
{
var self = this;
self.markup = $('<li>'
+ '<table class="questionGrid">'
+ '<thead><tr><th class="sortHandle"> </th>'
+ '</tr></thead><tbody></tbody></table>'
+ '<div class="optBar">'
+ '<input type="button" class="addQRow" value="Add question" />'
+ '<input type="button" class="addCol" value="Add column" />'
+ '<input type="button" class="delGroup" value="Delete grid" />'
+ '</div></li>');
self.questionBut = $('.addQRow', self.markup);
self.deleteBut = $('.delGroup', self.markup);
self.colBut = $('.addCol', self.markup);
self.rowCount = 0;
self.colCount = 0;
self.addQuestion = function ()
{
var thisCol = 0;
var newRow = $('<tr><td class="sortHandle"><label>test' +
(self.rowCount + 1) + '</label></td></tr>');
for (thisCol = 0; thisCol < self.colCount; thisCol++)
{
newRow.append ('<td><input type="radio" /></td>');
}
$('tbody', self.markup).append (newRow);
$('tbody', self.markup).sortable ('refresh');
return (++self.rowCount);
};
self.addColumn = function ()
{
$('thead tr', self.markup).append ('<th> </th>');
$('tbody tr', self.markup).append ('<td><input type="radio" /></
td>');
return (++self.colCount);
};
self.construct = function ()
{
self.questionBut.click (self.addQuestion);
self.colBut.click (self.addColumn);
target.append (self.markup);
$('tbody', self.markup).sortable ({
handle : '.sortHandle'
});
target.sortable ('refresh');
};
self.construct ();
}
*/
function QuestionGroup (target, title, subtitle)
{
var self = this;
self.questions = {};
self.title = title;
self.markup = $('<li id="'
+ title
+ '"><input type="hidden" name="groups[' + title + ']" />'
+ '<h3 class="sortHandle">' + (title || 'No title') + '</h3>'
+ '<h4>' + subtitle + '</h4>'
+ '<div class="fields"><ul class="questionList"></ul></div>'
+ '<div class="optBar">'
+ '<input type="button" class="addQ" value="Add question" />'
+ ' said:
+ '</div></li>');
self.questionList = $('.questionList', self.markup);
self.addQBut = $('.addQ', self.markup);
//self.addGridBut = $('.addGrid', self.markup);
//self.delGroupBut = $('.delGroup', self.markup);
self.addQuestion = function ()
{
// Using self in eval from here causes problems
activeTarget = self;
if (dialogs.newQ)
{
dialogs.newQ.dialog ('open');
}
else
{
dialogs.newQ = $('#cmsDgNewQ').show ().dialog ({
title : 'New Question',
modal : true,
buttons : {
'Create' : function (evt)
{
var label = $('#label', this).val ();
if (label)
{
var type = $('#qType').val ();
// The line
eval ('activeTarget.questions [label] = new ' + type +
' (activeTarget, label)');
console.log (activeTarget.questions);
}
$(this).dialog ('close');
}
}
});
}
};
self.addGrid = function ()
{
};
self.construct = function ()
{
target.append (self.markup);
self.addQBut.click (self.addQuestion);
self.questionList.sortable ({
handle : '.sortHandle'
});
};
self.construct ();
}
$(document).ready (function ()
{
var groupList = $('#cmsSurveyPreview').sortable ({
handle : '.sortHandle'
});
var groups = {};
var activeGroup;
var activeTarget;
// Prevent any IDs we're using in our editor from being assigned
$('body [@id]').each (function ()
{
groups [this.id] = true;
});
// Configure dialog overlay
$.extend ($.ui.dialog.defaults,
{
overlay : {
'background-color' : 'black',
'filter' : 'alpha(opacity=30)',
'opacity' : '0.3',
'-moz-opacity' : '0.3'
}
});
$.extend ($.ui.sortable.defaults,
{
distance : 5
});
$('#cmsAddGroup').click (function ()
{
if (dialogs.newGroup)
{
dialogs.newGroup.dialog ('open');
}
else
{
dialogs.newGroup = $('#cmsDgNewGroup').show ().dialog ({
title : 'New Question Group',
modal : true,
buttons : {
'Create' : function (evt)
{
var title = $('#groupTitle', this).val ();
var question = $('#groupQuestion', this).val ();
if ((title) && (!groups [title]))
{
groups [title] = new QuestionGroup (groupList, title,
question);
groupList.sortable ('refresh');
$(this).dialog ('close');
//console.log (groups);
}
},
'Cancel ' : function (){$(this).dialog ('close');}
}
});
}
});
/*
$('.addQ').livequery ('click', function ()
{
activeGroup = $('ul', $(this).parent ().parent ());
if (dialogs.newQ)
{
dialogs.newQ.dialog ('open');
}
else
{
dialogs.newQ = $('#cmsDgNewQ').show ().dialog ({
title : 'New Question',
modal : true,
buttons : {
'Create' : function (evt)
{
var label = $('#label', this).val ();
if (label)
{
var type = $('#qType').val ();
eval ('new ' + type + ' (activeGroup, label)');
}
$(this).dialog ('close');
}
}
});
}
});
*/
$('.addGrid').livequery ('click', function ()
{
activeGroup = $('ul', $(this).parent ().parent ());
//console.log (activeGroup);
new QuestionGrid (activeGroup);
});
$('.delGroup').livequery ('click', function ()
{
$(this).parent ().parent ().remove ();
});
$('.delQ').livequery ('click', function ()
{
$(this).parent ().remove ();
});
});
-->
</script>
</head>
<body>
<form action="<?php echo ($_SERVER ['PHP_SELF']); ?>" method="post">
<ul id="cmsSurveyPreview">
</ul>
<fieldset id="controls">
<input type="button" id="cmsAddGroup" value="Add group" />
<input type="submit" value="Done" />
</fieldset>
<div class="cmsDialog" id="cmsDgNewGroup">
<ul>
<li>
<label for="groupTitle">Title</label>
<input type="text" id="groupTitle" />
</li>
<li>
<label for="groupQuestion">Question</label>
<input type="text" id="groupQuestion" />
</li>
</ul>
</div>
<div class="cmsDialog" id="cmsDgNewQ">
<ul>
<li>
<label for="label">qType</label>
<select id="qType">
<option value="QuestionString">Single line</option>
<option value="QuestionText">Multi line</option>
<option value="QuestionCheck">Check box</option>
</select>
</li>
<li>
<label for="label">Label</label>
<input id="label" />
</li>
</ul>
</div>
</form>
<pre>
<?php
print_r ($_POST);
?>
</pre>
</body>
</html>