Wednesday, September 29, 2010

09/29: Add a Event Manager To SP Document Library

 

  • Download wsp file from :http://speventsmanager.codeplex.com/
  • In SP Server : 
    • stsadm -o addsolution -filename GatWeb.SharePoint.EventsManager.wsp
    • Extract wsp file to a folder , should see  two folders named “GatWebEventsManager” and “Template”
    • Copy the whole folder “GatWebEventsManager” to “c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\”
    • Copy the whole folder “Template” to  “c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\”
    • stsadm -o installfeature -name GatWebEventsManager
    • stsadm -o activatefeature -url <fill-in-yours> -name GatWebEventsManager .eg:  stsadm -o activatefeature -url http://win-bsrsp6ohs7r/ -name GatWebEventsManager

 

Uninstall :

 

stsadm.exe -o deletesolution -name MeetingTemplate.wsp

Thursday, September 23, 2010

0923:Agile Terms

 

Daily Stand-up meeting

Contiguous Integration

Pair Programming

TDD

0923: Javascript : window.event.returnValue = false

 

在浏览器事件中,会触发一些默认动作,比如:点击一个链接时,执行完捕获/冒泡动作后,会触发链接的默认事件:跳转到指定链接地址。
在很多时候,我们需要改变这些默认操作,比如:点击一个链接时,我们执行一些 ajax 操作,但是我们并不希望执行跳转动作,于是,就有了本文:阻止浏览器默认操作。 其实这并不是一个非常难的课题,单独拿出来的原因还是浏览器兼容问题:

 

 

  • <script type="text/javascript">

  •  

  • // 说明:Javascript 中阻止浏览器默认操作

  • // 作者:John Resig

  • // 来源:CodeBit.cn ( http://www.CodeBit.cn )

  •  

  • function stopDefault( e ) {

  • // Prevent the default browser action (W3C)

  • if ( e && e.preventDefault )

  • e.preventDefault();

  • // A shortcut for stoping the browser action in IE

  • else

  • window.event.returnValue = false;

  • return false;

  • }

  • </script>





  •  

    09/23: Using ODP.net without oracle client installation

     

    • Copy all following dll files to c:\windows\system32
      • Oracle.DataAccess.dll
      • oci.dll
      • ociw32.dll
      • orannzsbb11.dll
      • oraocci11.dll
      • oraociei11.dll
      • OraOps11w.dll
    • In .net project , adding reference from c:\windows\system32

    Wednesday, September 22, 2010

    09/22: Bit Operation

    • to check whether a value is a power of 2 (simplest way)
      • n is not 0
      • if((n&(n-1))==0 ) –> n is power of 2

    Reason :  if n is power of 2 , it only has one 1 in binary format . so n&n-1==0 except n=0

    Thursday, September 16, 2010

    09/16: Windows.Onload VS Document.body.Onload

     

    • document.body.onload is triggered when the body content is loaded . So it happens before windows.onload:

    document.body.onload>windows.onload

    • the method for body.onload can only be written under or after body tag.
    • the method for window.onload can be written anywhere.

    <html>
    <head>
    <title>Onload Example</title>
    </head>
    <body onload=”alert(‘Goodbye’)”> // can not written before <body..>
    </body>
    </html>

    Wednesday, September 15, 2010

    09/15/2010: Javascript :attachvent VS addeventhandler

     

    • attachEvent is only for IE ; addventhandler is not working for IE But firefox

    (ZT)

    举例:
    document.getElementById("btn").onclick = method1;
    document.getElementById("btn").onclick = method2;
    document.getElementById("btn").onclick = method3;
    如果这样写,那么将会只有medhot3被执行

    写成这样:
    var btn1Obj = document.getElementById("btn1");
    //object.attachEvent(event,function);
    btn1Obj.attachEvent("onclick",method1);
    btn1Obj.attachEvent("onclick",method2);
    btn1Obj.attachEvent("onclick",method3);
    执行顺序为method3->method2->method1

    如果是Mozilla系列,并不支持该方法,需要用到addEventListener
    var btn1Obj = document.getElementById("btn1");
    //element.addEventListener(type,listener,useCapture);
    btn1Obj.addEventListener("click",method1,false);
    btn1Obj.addEventListener("click",method2,false);
    btn1Obj.addEventListener("click",method3,false);
    执行顺序为method1->method2->method3

    使用实例:

    1。
    var el = EDITFORM_DOCUMENT.body;
    //先取得对象,EDITFORM_DOCUMENT实为一个iframe
    if (el.addEventListener)...{
    el.addEventListener('click', KindDisableMenu, false);
    } else if (el.attachEvent)...{
    el.attachEvent('onclick', KindDisableMenu);
    }
    2。
    if (window.addEventListener) ...{
    window.addEventListener('load', _uCO, false);
    } else if (window.attachEvent) ...{
    window.attachEvent('onload', _uCO);
    }

    Monday, September 13, 2010

    09/13/2010: Object Inheritance in Javascript

     

    • Hybrid constructor/prototype paradigm
    • Dynanuc prototying
    • Using  xbObjects (need an external xbObjects.js library file)

    Sample Code :

     

    test1.html:

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
         <script type="text/javascript" src="xbObjects.js"></script>
        <script language="javascript">

            // Example of hybrid constructor/prototype paradigm of object definition to show inheritance,
            // Class Definition for SuperClass Polygon
            function Polygon(sides)
            {
                this._sides = sides;
            }
            Polygon.prototype.GetArea = function()
            {
                return 0;
            }
            // Class Definition for subclass Trigangle

            function Trigangle(base, height)
            {
                Polygon.call(this, 3); // triangel 's sides=3
                this._base = base;
                this._height = height;

            }

            Trigangle.prototype = new Polygon();
            Trigangle.prototype.GetArea = function()
            {

                return 0.5 * this._base * this._height;
            }

            // Test Code

            var tra = new Trigangle(12, 4);
            alert(tra.GetArea());
            alert(tra._sides);
            // Example of Dynanuc prototying to show inheritance ( the purpose is to write all code including function inside of constructor)
            // superclass
            function Polygon1(iSides)
            {
                this.sides = iSides;

                if (typeof Polygon1._initialized == "undefined")
                {

                    Polygon1.prototype.getArea = function()
                    {
                        return 0;
                    };

                    Polygon1._initialized = true;
                }
            }

            // subclass
            function Triangle1(iBase, iHeight)
            {
                Polygon1.call(this, 3);
                this.base = iBase;
                this.height = iHeight;

                if (typeof Triangle1._initialized == "undefined")
                {

                    Triangle1.prototype.getArea = function()
                    {
                        return 0.5 * this.base * this.height;
                    };

                    Triangle1._initialized = true;
                }
            }

            Triangle1.prototype = new Polygon1();

            // Test
            var tra1 = new Triangle1(12, 4);
            alert(tra1.getArea());
            alert(tra1.sides);

            // By using xbObjects for oop in javascript
            // need to reference a js file called xbObjects.js
            // super class
            _classes.registerClass("Polygon2");

            function Polygon2(iSides)
            {

                _classes.defineClass("Polygon2", prototypeFunction);

                this.init(iSides);

                function prototypeFunction()
                {

                    Polygon2.prototype.init = function(iSides)
                    {
                        this.parentMethod("init");
                        this.sides = iSides;
                    };

                    Polygon2.prototype.getArea = function()
                    {
                        return 0;
                    };

                }
            }
            // subclass
            _classes.registerClass("Triangle2", "Polygon2");

            function Triangle2(iBase, iHeight)
            {

                _classes.defineClass("Triangle2", prototypeFunction);

                this.init(iBase, iHeight);

                function prototypeFunction()
                {
                    Triangle2.prototype.init = function(iBase, iHeight)
                    {
                        this.parentMethod("init", 3);
                        this.base = iBase;
                        this.height = iHeight;
                    };

                    Triangle2.prototype.getArea = function()
                    {
                        return 0.5 * this.base * this.height;
                    };
                }

            }
            // test

            var tra2 = new Triangle2(12, 4);

            alert(tra2.getArea());
        </script>
    </head>
    <body>

    </body>
    </html>

     

     

    BOM of Javascript

     

    BOM

    09/13/2010: Sharepoint Logging

     

    Can log message in Sharepoint event handler

    • Adding Microsoft.Officer.Server.dll reference
    • call :Microsoft.Office.Server.Diagnostics.PortalLog.LogString(….)
      • This will add an entry in a logfile of  C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS\…

    Friday, September 10, 2010

    09/10: Javascript Inheritance

     

    First option:constructor paradigm

    function ClassA(sColor)
            {
                this.color = sColor;
                this.sayColor = function()
                {
                    alert(this.color);
                };
            }

     

    function ClassB(sColor, sName)
         {

    // In javscript, a function name is just a pointer to function

    // So get Inheritance , use a pointer to ClassA first , then initialize it, after that delete

     
             this.newMethod = ClassA;
             this.newMethod(sColor);
             delete this.newMethod; // delete the reference
            // using call: ClassA.call(this.sColor) to replace the last three lines

    // useing apply: ClassA.apply(this, new Array(sColor)) to repalce the last three lines

     

    this.name = sName;
             this.sayName = function()
             {
                 alert(this.name);
             };
         }

     

    Test:

     

         var objA = new ClassA("red");
           var objB = new ClassB("blue", "Nicholas");
           objA.sayColor(); //outputs “red”
           objB.sayColor(); //outputs “blue”, A’s property
          objB.sayName(); //outputs “Nicholas” 
           alert(objB.color); // output “red” , A’s property

     

     

    Second option : prototype chaining

    function ClassA() {
    }
    ClassA.prototype.color = “red”;
    ClassA.prototype.sayColor = function () {
    alert(this.color);
    };

     

    function ClassB() {
    }
    ClassB.prototype = new ClassA();

    ClassB.prototype.name = “”;
    ClassB.prototype.sayName = function () {
    alert(this.name);
    };

     

    Test:

    // Note ClassA (parent) constructor can not have parameter

    var objA = new ClassA();
    var objB = new ClassB();
    objA.color = “red”;
    objB.color = “blue”;
    objB.name = “Nicholas”;
    objA.sayColor(); //outputs “red”
    objB.sayColor(); //outputs “blue”
    objB.sayName(); //outputs “Nicholas”

     

    Third option: Best solution

    function ClassA(sColor) {
    this.color = sColor;
    }
    ClassA.prototype.sayColor = function () {
    alert(this.color);
    };
    function ClassB(sColor, sName) {

    ClassA.call(this, sColor);
    this.name = sName;
    }
    ClassB.prototype = new ClassA();
    ClassB.prototype.sayName = function () {
    alert(this.name);
    };

    Test:

    var objA = new ClassA(“red”);
    var objB = new ClassB(“blue”, “Nicholas”);
    objA.sayColor(); //outputs “red”
    objB.sayColor(); //outputs “blue”
    objB.sayName(); //outputs “Nicholas”

    Friday, September 03, 2010

    09/03: Func/Action And Lambda Exprssion

     

    Func code:

     

    Func<string, bool> Istruefunc = username =>
                   {
                       return username == "yuyi";
                   };

     

    Equivalent Lambda expression:

    Expression<Func<string, bool>> IsTrue = username =>
                   username == "yuyi";

    Will receive error with following :

    Expression<Func<string, bool>> test = username =>
                    {
                        return username == "yuyi";
                    };

     

     

    But we can do this :

     

    public static bool method(string username)
            {
                return username == "yuyi";
            }

    Expression<Func<string, string>> test = username =>        
                     method(username);

    09/03: Closure in Anonymous /Lambda Expression

     

    1. What is closure ?

    Closure is a concept coming from functional programming. It can use variables which is outside of scope of current context.( in simple word, using  variables declared outside inside lambda expression ).

    闭包是将一些执行封装,将它像对象一样传递,在传递时,执行依然能够访问到原上下文。访问原来上下文,是闭包的重要特征

    eg.

     

    int mulitplyby = 2;

    Func<int, int> operation = x => x * mulitplyby

    //mulitplyby variable inside the lambda expression although it is declared outside the scope of the expression. This concept is called variable capture. C# complier will take these captured variables and put in a new generated helper class  .

    2. Confusion/Problem caused by closure

     

    class Program
        {
            static void Main(string[] args)
            {
                List<Action> ls = new List<Action>();
                List<Action<int>> ls2 = new List<Action<int>>();

                for (int i = 0; i < 10; i++)
                {
                    Action act = () =>
                    {
                        Console.WriteLine(i);
                    };

                    Action<int> act2 = (sdy) =>
                    {
                        int tcp = sdy;
                        Console.WriteLine(tcp);
                    };

                    ls.Add(act);
                    ls2.Add(act2);
                }

                foreach (Action action in ls)
                {
                    action();
                }

                foreach (Action<int> action in ls2)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        action(i);
                    }
                }

                System.Console.Read();
            }
        }

    // First result:  由于只声明了一个i变量
    所以所有的Action捕获的都是同一个i变量。结果就是每一行都输出数字10

     

    // Second Result : 式实现了输出0到9

    之所以使用tcp可以得到正确的结果,是因为tcp每次都重新开辟空间。

    与上例代码的唯一不是是在循环体中使用了一个局部变量tp,这种写法在通常看来不通是多用了一个中转变量,对程的执行不会有什么影响,但事实上tp这个变量在被每个Action独立保存. 这样,每次循环体在执行的时候,都会取得一个全新的tp,而且tp不会因为所在声名体的完成而出栈

     

     

     

    3.匿名方法中的变量 Scope:

     

    若匿名方法中如果引用了某个outside 变量,则该局部变量将被提升为实例变量,并储存于一个叫做闭包(closure)的对象中。提升之后,即使创建该变量的方法执行完毕该变量仍不会消亡。 当指向该匿名函数的所有引用都消失后,该闭包变量即可正常地被垃圾回收器回收

    eg.

            delegate int wxd(int i);
           delegate wxd lzm(int ii);

           static void Main(string[] args)
           {
               lzm obj = delegate(int ii)
               {
                   return
                   delegate(int i)
                   {
                       return i + ii;
                   };
               };
               wxd w1 = obj(1);
               wxd w2 = obj(2);

               System.Console.WriteLine(w1(3));
               System.Console.WriteLine(w2(3));

               System.Console.Read();
           }

     

    // Return 4 , 5

    通常理解,函数的参数是放在栈中的。
    如果闭包也将参数放在栈中,那么[ii]在[obj]运行结束的时候就会消失掉,这个时候[w1,w2]再通过栈去搜索[ii]显然就是不可能的。
    所以闭包中参数或内部变量不能放在栈中.而是放在程序执行过程之中的一张全局表里. [[obj]在返回内部函数的时候,将全局表,自己的结构表,内部函数的指针一起传递给变量[w1,w2].
    这时内部函数可以访问[ii],外部却无法访问[ii]