Thursday, October 23, 2014

ng-readonly : Token '-100' is an unexpected token at column 10 of the expression

Token '-100' is an unexpected token at column 10 of the expression [user.Id!=='-100'] starting at ['-100'
 
 
ng-readonly="user.Id!=='-100'"
 
The issue disappears if the code changes to
 
    ng-readonly="user.Id!='-100'"
 Or ng-readonly="user.Id!==-100"

The reason?

ng-readonly or ng-show takes an "AngularJS statement." This type of statement only has an == operator, but this operator behaves like ===. It's a bit confusing.

Type coercion means that when the operands of an operator are different types, one of them will be converted to an "equivalent" value of the other operand's type. For instance, if you do:
boolean == integer
the boolean operand will be converted to an integer: false becomes 0true becomes 1. Then the two values are compared.
However, if you use the non-converting comparison operator ===, no such conversion occurs. When the operands are of different types, this operator returns false, and only compares the values when they're of the same type.

Tuesday, October 14, 2014

ORA-01722: invalid number - Parameters should always be added in order

I got this error while run the update statement.

The issue is due to the order while adding parameters not consistent with the order of the sql.
        const string UpdateXXXXXXXQuery = @"UPDATE XXXXXXX
                                                    SET STATUS           = :v_STATUS,
                                                    STATUSDATE       = SYSDATE,
                                                    STATUSUPDATEUSER = :v_USER                            
                                                    WHERE COLYEAR        = :v_YEAR
                                                    AND EMPLOYERNUMBER   = :v_EMPLOYER";
Parameters should always be added in the order of the sql
            OracleCommand cmd = new OracleCommand();
            OracleDataReader odr = null;
            cmd.Connection = dataDBConnection;
            cmd.CommandText = UpdateXXXXXXXQuery;
            cmd.CommandType = CommandType.Text;
            cmd.Prepare();
            cmd.Parameters.Clear();
            cmd.Parameters.Add("v_STATUS", "SUBMITTED");//1st
            cmd.Parameters.Add("v_USER", userLogin);//2nd
            cmd.Parameters.Add("v_YEAR", OracleDbType.Int32, DateTime.Now.Year, ParameterDirection.Input);//3rd
            cmd.Parameters.Add("v_EMPLOYER",OracleDbType.Int32,  employerID,ParameterDirection.Input);//4th            
            cmd.ExecuteNonQuery();
            cmd.Dispose();

Monday, October 13, 2014

Creating Shorthand/Literal Values from Constructors

JavaScript provides shortcuts—called “literals”—for manufacturing most of the native object values without having to use new Foo() or new Bar(). For the most part, the literal syntax accomplishes the same thing as using the new operator. The exceptions are: Number(), String(), and Boolean()

var myRegExp = new RegExp('\bt[a-z]+\b');
var myRegExpLiteral = /\bt[a-z]+\b/;

var myObject = new Object();
var myObjectLiteral = {};

var myArray = new Array('foo', 'bar');
var myArrayLiteral = ['foo', 'bar'];

var myFunction = new Function("x", "y", "return x*y");
var myFunctionLiteral = function(x, y) {return x*y};

var myFunction = new Function();
var myFunctionL = function() {}; // literal shorthand

var myObject = new Object();
var myObjectL = {}; // literal shorthand
var myArray = new Array();
var myArrayL = []; // literal shorthand

var myNumber = new Number(23); // an object
var myNumberLiteral = 23; // primitive number value, not an object

var myString = new String('male'); // an object
var myStringLiteral = 'male'; // primitive string value, not an object

var myBoolean = new Boolean(false); // an object
var myBooleanLiteral = false; // primitive boolean value, not an object
var CustomConstructor = function CustomConstructor(){ return 'Wow!'; };
var instanceOfCustomObject = new CustomConstructor();
// logs true
console.log(instanceOfCustomObject.constructor === CustomConstructor);
// returns a reference to CustomConstructor() function
// returns 'function() { return 'Wow!'; };'
console.log(instanceOfCustomObject.constructor);

using literals simply conceals the underlying process identical to using the new operator. Maybe more importantly, it’s a lot more convenient!

JavaScript Enlightenment :
When using literal values for string, number, and boolean, an actual complex object is never created until the value is treated as an object. In other words, you are dealing with a primitive datatype until you attempt to use methods or retrieve properties associated with the constructor (e.g., var charactersInFoo = 'foo'.length). When this happens, JavaScript creates a wrapper object for the literal value behind the scenes, allowing the value to be treated as an object. Then, after the method is called, JavaScript discards the wrapper object and the value returns to a literal type. This is why string, number, and boolean are considered primitive (or simple) datatypes. I hope this clarifies the misconception that “everything in JavaScript is an object” with the concept that “everything in JavaScript can act like an object.”

When a primitive value is used as if it were an object created by a constructor, JavaScript converts it to an object in order to respond to the expression at hand, but then discards the object qualities and changes it back to a primitive value.

// Produce primitive values
var myNull = null;
var myUndefined = undefined;
var primitiveString1 = "foo";
var primitiveString2 = String('foo');//did not use new, so we get primitive
var primitiveNumber1 = 10;
var primitiveNumber2 = Number('10');//did not use new, so we get primitive
var primitiveBoolean1 = true;
var primitiveBoolean2 = Boolean('true');//did not use new, so we get primitive
/* Access the toString() property method (inherited by objects from
object.prototype) to demonstrate that the primitive values are converted to
objects when treated like objects. */
// logs "string string"
console.log(primitiveString1.toString(), primitiveString2.toString());
// logs "number number"
console.log(primitiveNumber1.toString(), primitiveNumber2.toString());
// logs "boolean boolean"
console.log(primitiveBoolean1.toString(), primitiveBoolean2.toString());
/* This will throw an error and not show up in firebug lite, as null and
undefined do not convert to objects and do not have constructors. */
console.log(myNull.toString());
console.log(myUndefined.toString());


Math is a static object—a container for other methods—and is not a constructor that uses the new operator

It’s possible to forgo the use of the new keyword and the concept of a constructor function by explicitly having the function return an object. The function would have to be written explicitly to build an Object() object and return it:
var myFunction = function(){
return {prop: val}
};
Doing this, however, sidesteps prototypal inheritance.

Wednesday, October 8, 2014

Oracle: An attempt was made to modify an object, REF, VARRAY, nested table, or LOB column type

Here is what you can do to change a column of type VARCHAR2(4000) to a CLOB since you will get the error if simply using 'alter table EMAIL modify (TEMPLATE_CONTENT clob)':

alter table EMAIL  add (temp clob);

update EMAIL set temp=EMAIL_BODY, EMAIL_BODY=null;

alter table EMAIL drop column EMAIL_BODY;
alter table EMAIL rename column temp to EMAIL_BODY;

Tuesday, September 30, 2014

Telerik Reporting: set subreport datasource the same as main report

Telerik had some posts regarding this, however the post is not clear enough so I have to spend some time to figure out myself.

Here are the steps I have made the subreport datasource dynamically configured (Telerik Reporting Q1 2014) successfully:

Step 1. Define a static method in your factory class:
public static class YourFactoryClass{
        public static Telerik.Reporting.SqlDataSource SetConnectionString(Telerik.Reporting.SqlDataSource dataSource)
        {
 
            var dataDBConnectionString = _get_DB_connect_str();
            dataSource.ConnectionString = dataDBConnectionString;
            return dataSource;
        }
 }

Step 2. Expose your data with custom User Functions, you have to use the AssemblyReferences Element of the Telerik.Reporting configuration section to reference your custom assembly:
in your web.config or app.config:
<configuration>
      <configsections>
          <section allowdefinition="Everywhere" allowlocation="true" name="Telerik.Reporting" type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting">
          </section>

      </configsections>
      …
       <telerik .reporting="">
          <assemblyreferences>
              <add culture="neutral" name="YourCustomAssembly_Name" publickeytoken="null" version="1.0.0.0"/>              
          </assemblyreferences>        
       </Telerik.Reporting>
</configuration>


Step 3. Add DataSource binding in your subreport ( not the main report) data items property. In Report Designer, right click anywhere other than those fields defined, find 'Bindings' inside Properties panel then launch the 'Edit Bindings' dialogue box,  and 'DataSource' should be in the Property path dropdown so just select it from dropdown:

Property path: DataSource
Expression: =XXXNameSpace.YourFactoryClass.SetConnectionString(ReportItem.ItemDefinition.DataSource)

Step 4. Rebuild your project/solution, have it a try. It should hit the static method defined ( to dynamically update the connection string for subreport ) in the step 1 if you run in debugging mode.

That is it.

Sunday, September 28, 2014

JavaScript: what result this will render

var data = 'mytest';
function showSomething() {
    console.log(data);    
    var data = 'newData';
    console.log(data);    
}

showSomething();
How about this:
var data = "Rafael Nadal";
(function () {    
    console.log("the guy was " + data);
    var data = "Roger Federer";

    console.log("the guy is " + data);
})();
JavaScript turns our function declaration into a function expression and hoists it to the top. JavaScript applies different rules when it comes to function hoisting depending on whether you have a function expression or a function declaration. A function declaration is fully hoisted while a function expression follows the same rules as variable hoisting.

JavaScript treats variables which will be declared later on in a function differently than variables that are not declared at all. Basically, the JavaScript interpreter "looks ahead" to find all the variable declarations and "hoists" them to the top of the function.

JavaScript: function method bind()

From 'JavaScript- The Definitive Guide' but with some of my own meat
this is a JavaScript keyword, not a variable. Unlike variables  [variables declared within a function are visible throughout the function (including within nested functions) but do not exist outside of the function.] , the this keyword does not have a scope, and nested functions do not inherit the this value of their caller. If a nested function is invoked as a method, its this value is the object it was invoked on. If a nested function is invoked as a function then its this value will be either the global object (non-strict mode) or undefined (strict mode). It is a common mistake to assume that a nested function invoked as a function can use this to obtain the invocation context of the outer function. If you want to access the this value of the outer function, you need to store that value(this) into a variable that is in scope for the inner function. It is common to use the variable self for this purpose.

For example:
var o = { // An object o.
          m: function() { // Method m of the object.
                   var self = this; // Save the this value in a variable.
                   console.log(this === o); // Prints "true": this is the object o.
                   f(); // Now call the helper function f().

                   function f() { // A nested function f
                        console.log(this === o); // "false": this is global or undefined
                        console.log(self === o); // "true": self is the outer this value.
                   }
          }
};
o.m(); // Invoke the method m on the object o.
Console:
true 
false 
true 
 // A step further
 var o = { // An object o.
          m: function() { // Method m of the object.

                   //every function invocation has a this value, and a
                   //closure cannot access the this value of its outer function 
                   //unless the outer function has saved that value into a variable
                   var self = this; // Save the this value in a variable.
                   console.log(this === o); // Prints "true": this is the object o.
                   f.bind(this)(); // Bind f to o using 'this' then call f().

                   function f() { // A nested function f
                        console.log(this === o); // "true": this is now bound to o
                        console.log(self === o); // "true": self is the outer this value.
                   }
           }
};
o.m(); // Invoke the method m on the object o
Console:
true 
true 
true  
            // Credit to Richard Of Stanley
            // This data variable is a global variable
            var data = [
                {member:"R. Nadal", age:28},
                {member:"R. Federer", age:33}
            ]

            var user = {
                // local data variable
                data    :[
                    {member:"L. James", age:28},
                    {member:"M. Jordan", age:53}
                ],
                viewData:function (event) {
                    var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1

                    console.log (this.data[randomNum].member + " " + this.data[randomNum].age);
                }

            }

            // Assign the viewData method of the user object to a variable
            var viewDataVar = user.viewData;

            viewDataDataVar(); // R. Nadal 28  (from the global data array, not from the local data array)
Console:
R. Nadal 28 
When we execute the viewDataVar () function, the values printed to the console are from the global data array, not the data array in the user object. This happens because viewDataVar () is executed as a global function and use of this inside viewDataVar () is bound to the global scope, which is the window object in browsers.
            // A step further
            // This data variable is a global variable
            var data = [
                {member:"R. Nadal", age:28},
                {member:"R. Federer", age:33}
            ]

            var user = {
                // local data variable
                data    :[
                    {member:"L. James", age:28},
                    {member:"M. Jordan", age:53}
                ],
                viewData:function (event) {
                    var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1

                    console.log (this.data[randomNum].member + " " + this.data[randomNum].age);
                }
              
            }
             //Invoke the method viewData on the object user
             user.viewData(); //from the local data array 
            
             var viewDataDataVar = user.viewData;  
             viewDataDataVar();//from the global data array
Console:
L. James 28
R. Nadal 28 
When we execute the user.viewData() , the values printed to the console are from the data array in the user object. This happens because user.viewData() is executed as a method of user(Yes, a method of user object, I am right about this) and use of this inside user.viewData() is bound to the user object.
            // This data variable is a global variable
            var data = [
                {member:"R. Nadal", age:28},
                {member:"R. Federer", age:33}
            ]

            var user = {
                // local data variable
                data    :[
                    {member:"L. James", age:28},
                    {member:"M. Jordan", age:53}
                ],
                viewData:function (event) {
                    var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1

                    console.log (this.data[randomNum].member + " " + this.data[randomNum].age);
                }

            }

            // Bind the viewData method to the user object
            var viewDataDataVar = user.viewData.bind(user);
            //Now the we get the value from the user object because the this keyword is bound to the user object
            viewDataDataVar();
Console:
L. James 28  

var myObj = {
    _Function: function () {},
    __Function: function () {},

    getAsyncData: function (cb) {
        cb();
    },

    render: function () {
        var self = this;
        this.getAsyncData(function () {
            this._Function();//ERROR
            self.__Function();//OK
        });
    }
};

myObj.render();
Console:
Uncaught TypeError: Object [object global] has no method '_Function' 

We need to keep the context of the myObj object referenced for when the callback function is called. Calling self._Function() enables us to maintain that context and correctly execute our function.
var myObj = {
    _Function: function () { 
      console.log('_Function');
    },
    __Function: function () {
      console.log('__Function');
    },

    getAsyncData: function (cb) {
        cb();
    },

    render: function () {
        var self = this;
        this.getAsyncData(function () {
            self._Function();//No more Error
            self.__Function();
        });
    }
};

myObj.render();
However, this could be neatened somewhat by using Function.prototype.bind(). Let’s rewrite our example:
var myObj = {
    _Function: function () { 
      console.log('_Function');
    },
    __Function: function () {
      console.log('__Function');
    },

    getAsyncData: function (cb) {
        cb();
    },

    render: function () {
      this.getAsyncData(function () {
         this._Function();
         this.__Function();
    }.bind(this));

  }
};

myObj.render();
Console:
_Function
__Function