Category Archives: Javascript

Test of Typescript SyntaxHilighter 1.5

 

//#region person class
class Person {
  @memoize('test')  //Typescript 1.5 decorators
  get name() { return '${this.first} ${this.last}'; }
  //test
  set name(val) {
    let [first, last] = val.split(' ');
    this.first = first;
    this.last = last;
  }
  greeting: string;
  constructor(message: string) {
    this.greeting = message;
  }
  //test of comment with a > and a < in it. Plus a #. Plus @test2
  greet(@anotherDecorator z : string) : string {
    var x = greeting || test;
    var t = 12 + 3;
    var b = !!x;
    $(".js-alert-link").on("click", (event:JQueryEventObject) => this.startTimer(event));
    return "Hello, " + this.greeting;
  }
}
//#endregion


YUI COMPRESSOR SINGLE FILE GENERATOR FOR VS2013

In the past I used a Javascript and CSS minifier in Visual Studio that I built on top of the original YUI compressor.  I implemented it via a Custom Tool / Single File Generator.  I figured this might be helpful with minifying the output of TypeScript.  I wanted to eliminate the Java dependency however, so  I built the generator on top of YUI Compressor.NET.  Unfortunately, this doesn’t seem to work for my situation.  When TypeScript generates new Javascript output it doesn’t seem to trigger the SFG to do its thing.   In any case I put the source on Github for anyone who is interested.

TYPESCRIPT CONVERSION A MONTH LATER

I’d like to report that the port of my project to TypeScript has been a success, so far the issues encountered have been minimal.  One issue I did bump into recently was due to a typo on my part.  I had used a variable in a small method as both a string and number at different points, like so

var x = parseInt(somestring);

...

x = x.toString();

During the conversion to TypeScript, I resolved the type errors caused by this by creating a string and a numeric variable like so

var xn : number = parseInt(somestring);

...

var xs:string = xs.toString();

but notice the typo, I did xs.toString() and assigned it to xs itself, not xn.toString().  This does not cause any compilation errors in TypeScript, but the code is essentially a guaranteed null reference error at run-time.  In my case it was a month before the little used method was executed.

DEBUGGING BOOKS 2

So I slacked off a little on my reading Pro AngularJS and also detoured a bit into reading up on Bootstrap since I am relatively inexperienced with it and the book more or less assumes you know it (or will at least ignore it 🙂 ).

I started reading through Chapter 7 and encountered some issues with the following code:

angular.module(&quot;sportsStore&quot;)
    .constant(&quot;dataUrl&quot;, &quot;http://localhost:5500/products&quot;)
    .controller(&quot;sportsStoreCtrl&quot;, function ($scope, $http, dataUrl) {

        $scope.data = {};

        $http.get(dataUrl)
            .success(function (data) {
                $scope.data.products = data;
            })
            .error(function (error) {
                $scope.data.error = error;
            });
    });

In the section “Handling Ajax Errors” the author states “The object passed to the error function defines status and message properties.”.  Actually this is not the case, the error function should have the signature

.error(function (data, status, headers, config) {

Also, the first parameter is not an error object, it is the response data. For example in the case of a 404 error it would be the returned HTML 404 page.

The following code should make the example work


            .error(function (data, status, headers, config) {
                $scope.data.error = {};
                $scope.data.error.data = data;
                $scope.data.error.status = status;
            });

Secondly to test the error handling and achieve a 404 error, the author states in a tip to change the URL to something like ‘http://localhost:5500/doesNotExist’. This will most likely not work , as the site is running under nodejs on port 5000, and Deployd is hosting under port 5500, the browser will see this as a CORS request and most likely not return any error information, similar to the angular issue described in the link below. If you want to get a 404 error change the URL to a non-existent relative URL something like ‘/doesNotExist’, this will be relative to the site URL and be a non-CORS request.

CORS Error Response Status = 0

https://github.com/angular/angular.js/issues/2798