Friday, 1 August 2014

More powerful Mustache template functions (lambdas)

The following insight allows the "helper" functions (lambdas) in Mustache to be more powerful by giving them access to much more data.

The standard shape of a Mustache template function is as follows:

{
"a":10,
"b":20,
"myfn": function(){
    return function(text, render){
        return "The cost is:" + render(text)
    }
},
"currency_symbol":"£"
}

The value of the parameter "text" will be the Mustache bracketed name of some part of the hash which was specified in the template ( e.g. "{{a}}" ). The purpose of the render function is to "lookup" the value of "a" in the hash and return 10 (in this case).

The insight recorded in this post is that any other simple value from the hash can be looked up and the value used in the function, for example...

return "The cost is: + render("{{currency_symbol}}") + render(text)

Tuesday, 29 October 2013

Coping with Browser Cache in AJAX calls

When using jQuery to perform AJAX calls the following is typical

$.get("myScript.php",{'arg1':val1,'arg2':val2},function(json){
  ...
},'json')

I have found that the browser can supply an old result from its cache when I want a new result to be evaluated.

The solution is to add another argument to the cgi pack which contains a value which is guaranteed to be 'new'. This will stop the browser using a cached result. The value of the argument is conveniently supplied by the Javascript Date package. Of course myScript.php can just ignore the extra argument.

$.get("myScript.php",{'arg1':val1,'arg2':val2,'antiCache':antiCache()},function(json){
  ...
},'json')

function antiCache(){
return (new Date()).getTime();
}

[The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.]

Tuesday, 23 October 2012

EXTRACT-ing results from Pear DB into separate variables


I was used to writing this sort of code when using php_mysql

$result = $db->getRow($sql);
list($surname,$initials,$login,$email,$tutor,$prog)=$result;
//now got separate variables $surname, $initials, etc

Now that I am using PEAR DB, list(...) does not work, so I have found another 'trick' using the PHP function extract

extract($result,EXTR_PREFIX_ALL, "student");
//student is used as a prefix
//now got variables like $student_surname, $student_initials, etc

Note:
I am using 
$db->setFetchMode(DB_FETCHMODE_ASSOC);
so the alternative is to access the names as
$result['surname'], $result['initials']

Friday, 12 October 2012

Parsing the Search String/Query String (revised)

This version avoids using the eval() function and so is neater and safer.
[Compared with the version given in an earlier note in this blog]

var GLOBAL={}

location.search.replace(
  new RegExp("([^?=&]+)(=([^&]*))?", "g"),
  function($0, $1, $2, $3) {
    GLOBAL[$1]=$3;
  }
);

Monday, 9 July 2012

Using jQuery datepicker with html5 contenteditable


You are supposed to use jQuery datepicker with an input element.
Trying to attach it to a div gives a stay open calendar.
The trick here is to attach the datepicker to a hidden input element.

Use the "trigger by button" feature (hence little 'calendar' graphic)

When the datepicker closes (onClose) the new value can be planted (.html() )  in the visible contenteditable element.

If you are watching "on focus" & "on blur" of the contenteditable (to catch user editing) then these events can be simulated as the value is entered (.focus() - .blur() ) so that the change in date is dealt with as if the user entered it themselves.

HTML code:

<div>
<span class='date' contenteditable='false'>9/7/2012</span>
<input type='hidden' class='datepicker' />
</div>
Javascript code:

$(".datepicker").datepicker({
  dateFormat: 'dd/mm/yy',
  showOn: "button",
  buttonImage: "images/calendar.gif",
  buttonImageOnly: true,
  onClose: function(dateText, inst) {
  $(this).parent().find("[contenteditable=true]").focus().html(dateText).blur();
  }
});

Wednesday, 6 June 2012

Deleting HTML5 database tables in Safari

If you are developing using HTML5 databases in Safari (SQLite) you may need to delete tables. In OSX Safari you can use Develop|"Show Web Inspector", then Resources, Databases. There you will have a type in window where you can enter SQL statements like "DROP TABLE ".

In iOS you have to rely on Settings, Safari, "Clear Cookies & Data"

Thursday, 12 January 2012

Appleworks drawings after Lion

Appleworks drawings after Lion

Appleworks does not (& will not) run under Lion

There are many solutions to many of the document types but...
There are many postings saying that the only real solution for Appleworks drawings is to buy EazyDraw

I want to offer a possible workaround - using Keynote
(N.B. this relies on doing something BEFORE) you migrate to Lion

1) Using Appleworks, make a new presentation and paste some of your Appleworks drawings in, one drawing to a slide
2) Now save the presentation and then open it in Keynote

Hey Presto, your diagrams are all there and you can ungroup them and edit them (if you know how to use Keynote, that is)

Problems?

I found that Keynote undersizes the text boxes so they need stretching to the right to stop the word wrap

I found that my arcs (quarter circles for the most part) had gained "radii" making them look like segments of a circle. (Hence the previous post on how to draw arcs in Keynote)