[Spark View Engine] #74: Add an attribute to declare a "just once" flag

12 views
Skip to first unread message

Spark View Engine

unread,
Oct 2, 2008, 4:07:06 AM10/2/08
to spar...@googlegroups.com
#74: Add an attribute to declare a "just once" flag
---------------------------+------------------------------------------------
Reporter: louis.dejardin | Owner:
Type: enhancement | Status: new
Priority: major | Component: Spark Core
Keywords: |
---------------------------+------------------------------------------------
I noticed a few times I've had to declare a variable, and add a test, and
then set the variable, when I had an element or piece of content I only
wanted to be included one time.

Something like <script type="text/javascript"
src="~/content/js/jquery-1.2.6.js" once="jquery"/> could be put several
places. Or there could be a partial used many times that has a script or
inline stylesheet that should only be on the page at most one time.

Or maybe ifonce="name"... Nah I think once="name" would be fine.

--
Ticket URL: <http://dev.dejardin.org/trac/spark/ticket/74>
Spark View Engine <http://dev.dejardin.org/trac/spark>
Spark View Engine

Артём Тихомиров

unread,
Oct 2, 2008, 6:31:08 AM10/2/08
to spar...@googlegroups.com
I've ended up with following solution.
In Shared/_global.spark:
<!-- Service globals - do not use directly -->
<global __CssReferences="new OrderedSet[[string]]()" type="ISet[[string]]"/>
<global __JsReferences="new OrderedSet[[string]]()" type="ISet[[string]]"/>
<global __OnReadyStatements="new List[[string]]()" type="IList[[string]]"/>
<!-- End of service globals declaration -->

<macro name="WriteJsReferencesDown">
    <for each="var fileName in __JsReferences">
        <script type="text/javascript" src="~/Content/${fileName}.js"></script>
    </for>   
</macro>

<macro name="WriteCssReferencesDown">
    <for each="var file in __CssReferences">
        <link rel="stylesheet" href="~/Content/${file}.css" type="text/css" />
    </for>   
</macro>

<macro name="WriteOnReadyStatements">
    <script type="text/javascript" if="__OnReadyStatements.Count > 0">
        jQuery(document).ready(
            function() {
                <for each="var statement in __OnReadyStatements">
                    ${statement}
                </for>
            }
        );
    </script>
</macro>

<macro name="css" files="params string[]">
    <for each="var file in files">
        # __CssReferences.Add(file);
    </for>
</macro>

<macro name="jQuery" additionalFiles="params string[]">
    # __JsReferences.Add("jQuery/jquery-1.2.6.min");
    <for each="var file in additionalFiles">
        # __JsReferences.Add("jQuery/" + file);
    </for>   
</macro>

<macro name="js" additionalFiles="params string[]">
    <for each="var file in additionalFiles">
        # __JsReferences.Add(file);
    </for>   
</macro>

<macro name="onReady" jsText="string">
    # if(!System.String.IsNullOrEmpty(jsText.Trim()))
    #        __OnReadyStatements.Add(jsText);
</macro>

In Shared/Application.spark:
<html>
    <head>       
        <title></title>
        <use content="pageHead" />
        ${WriteCssReferencesDown()}
        ${WriteJsReferencesDown()}
        ${WriteOnReadyStatements()}
    </head>
    <body>
    </body>
</html>

In any *.spark file using default maser-layout:
    ${css("jQuery/clockpick.1.2.4", "jQuery/datepicker/ui.datepicker")}
    ${jQuery("jquery-ui-1.6b.min", "i18n/ui.datepicker-ru", "jquery.clockpick.1.2.4.min")}

Louis DeJardin

unread,
Oct 2, 2008, 2:31:45 PM10/2/08
to Spark View Engine Dev
Very cool! I especially like the use params string[] in a macro.

You could probably also have an _onReady.spark file capture the body
as a temp variable:

<content var="jsText"><render/></content> ${onReady(jsText)}

So you could have:

<onReady>
$(stuff)
.hover(function() {etc})
.click(function() {etc});
</onReady>

if you didn't want to have to worry about string-escaping your
javascript in an
> <archive.sp...@gmail.com>wrote:

Артём Тихомиров

unread,
Oct 2, 2008, 4:03:20 PM10/2/08
to spar...@googlegroups.com
Yap, I have it - just skip it to simplify sample.  I've a problem though. If I Write following code:

<RegisterOnReadyStatement>
  if (1 > 2)
    jQuery("#some-id").Append("<img alt='message' src='uri'></img>");
</RegisterOnReadyStatement>

I ending up with parsing error (because of angle brackets and quotes, I suppose). But if I escaping special characters like this:

<RegisterOnReadyStatement>
  if (1 ]] 2)
    jQuery("#some-id").Append("[[img alt=\'message\' src=\'uri\']][[img]]");
</RegisterOnReadyStatement>

... Spark do not convert them backwards. So I've used this workarond:


<content var="jsText"><render/></content>
${onReady(jsText.Replace("]]", ">").Replace("[[", "<") )}


2008/10/2 Louis DeJardin <Louis.D...@gmail.com>

Chad Lee

unread,
Oct 2, 2008, 4:10:19 PM10/2/08
to spar...@googlegroups.com
I've run into this problem before as well.  When using javascript in a spark view, spark doesn't like the angle brackets.

My workaround was to do this:

if (1 ${'>'} 2)

Louis DeJardin

unread,
Oct 2, 2008, 4:52:29 PM10/2/08
to spar...@googlegroups.com
I think in that the <img alt='message' src='uri'> is being treated like an element, and the rest of the javascript (quotes included) is treated like any normal text. Attributes are always written with out with double-quotes, regardless of what was used in the spark file, so your javascript would end up having unescaped doublequotes:


jQuery("#some-id").Append("<img alt="message" src="uri"></img>");

Try reversing your use of javascript quotes to see if the parsing error goes away:


jQuery('#some-id').Append('<img alt="message" src="uri"></img>');

The ${'<'} would work as well.

Артём Тихомиров

unread,
Oct 3, 2008, 5:04:38 AM10/3/08
to spar...@googlegroups.com
Louis, parsing  error occirs in if operator above...

Spark View Engine

unread,
Oct 4, 2008, 2:53:00 AM10/4/08
to spar...@googlegroups.com
#74: Add an attribute to declare a "just once" flag
-----------------------------+----------------------------------------------
Reporter: louis.dejardin | Owner:
Type: enhancement | Status: closed
Priority: major | Component: Spark Core
Resolution: fixed | Keywords:
-----------------------------+----------------------------------------------
Changes (by louis.dejardin):

* status: new => closed
* resolution: => fixed

Comment:

(In [198]) closes #74 Add an attribute to declare a "just once" flag
adds <test once="flagname"> element and once="flagname" attribute support.
also includes a new bool Once(flagname) method on the base view class
which can be used independently.

--
Ticket URL: <http://dev.dejardin.org/trac/spark/ticket/74#comment:1>
Reply all
Reply to author
Forward
0 new messages