Thứ Tư, 15 tháng 1, 2014

Isolate scope trong directive argular js

Cái này lúc đầu rất khó hiểu,có thể giải thích đơn giản như sau:

<!doctype html>
<html ng-app="docsIsolateScopeDirective">
<head>
<script src="http://code.angularjs.org/1.2.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="Ctrl">
<my-customer info="naomi"></my-customer>
<hr>
<my-customer info="igor"></my-customer>
</div>
</body>
</html>


  Bắt đầu tag của directive chúng ta cần khai báo các biến mà ta muốn truyền từ parent scope xuống child scope, info = "naomi"  tức là ta muốn biến naomi của parent scope được truyền xuống children scope theo ba cách.
Cách 1:
 {
   scope:{
    hear:"@info"
   }
}
Ta muốn biến naomi mà thuộc tính info tham chiêu tới sẽ được truyền từ biến naomi của scope parent xuống scope child  thông qua biến hear, như vậy trong template của directive ta chỉ cần ghi {{hear}} thì biến hear trong children scope sẽ cập nhật giá trị của biến naomi của scope parent
Cách 2:
  {
   scope:{
    hear:"=info"
   }
}
Cách này cũng giống như trên nhưng chúng ta muốn biến hear naomi phải cùng tham chiếu tới nhau(2 two way binding) tức là cái này thay đổi thì cái kia cũng thay đổi.
Cách 3:


<my-component attribute-foo="{{foo}}" binding-foo="foo"


isolated-expression-foo="updateFoo(newFoo)" >

<input ng-model="isolatedFoo">
<button class="btn" ng-click="isolatedExpressionFoo({newFoo:isolatedFoo})">Submit</button>


.directive('myComponent', function () {
return {
scope:{
isolatedExpressionFoo:'&'
}
};
}).controller('MyCtrl', ['$scope', function ($scope) {
$scope.updateFoo = function (newFoo) {
$scope.foo = newFoo;
}
}]);



Cách này được áp dụng khi chúng ta muốn từ child scope ,chúng ta có thể sử dụng một biểu thức hoặc một hàm của scope parent.
Như trên chúng ta cần khai báo :

isolated-expression-foo="updateFoo(newFoo)",
isolated-expression-foo là một định danh để chúng ta khai báo rằng biến isolatedFoo sẽ được truyền tới newFoo nằm trong hàm updateFoo(newFoo) như khai báo ban đầu,để từ đây chúng ta có thể thực thi hàm updateFoo từ scope parent trong controller ,qua đó cập nhật tất cả các biến foo trong parent scope.

Thứ Sáu, 17 tháng 5, 2013

Đại cương về regular expression php


Question mark in regular expression (?i)
Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the right of the modifier. You can turn off modes by preceding them with a minus sign. All modes after the minus sign will be turned off. E.g. (?i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.

Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don't support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.

You can quickly test how the regex flavor you're using handles mode modifiers. The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.

?i means that everything following these characters should be matched case-insensitive.
Looking Inside the Regex Engine
Let's see what happens when we apply the regex \bis\b to the string This island is beautiful. The engine starts with the first token \b at the first character T. Since this token is zero-length, the position before the character is inspected. \b matches here, because the T is a word character and the character before it is the void before the start of the string. The engine continues with the next token: the literal i. The engine does not advance to the next character in the string, because the previous regex token was zero-width. i does not match T, so the engine retries the first token at the next character position.

\b cannot match at the position between the T and the h. It cannot match between the h and the i either, and neither between the i and the s.

The next character in the string is a space. \b matches here because the space is not a word character, and the preceding character is. Again, the engine continues with the i which does not match with the space.

Advancing a character and restarting with the first regex token, \b matches between the space and the second i in the string. Continuing, the regex engine finds that i matches i and s matches s. Now, the engine tries to match the second \b at the position before the l. This fails because this position is between two word characters. The engine reverts to the start of the regex and advances one character to the s in island. Again, the \b fails to match and continues to do so until the second space is reached. It matches there, but matching the i fails.

But \b matches at the position before the third i in the string. The engine continues, and finds that i matches i and s matches s. The last token in the regex, \b, also matches at the position before the third space in the string because the space is not a word character, and the character before it is.

The engine has successfully matched the word is in our string, skipping the two earlier occurrences of the characters i and s. If we had used the regular expression is, it would have matched the is in This.

Thứ Bảy, 13 tháng 4, 2013

So sánh class php dùng reflection

Đây là class các bạn dùng:
<?php
class compareobject
{
public static function compareobjects($obj1,$obj2)
{
$resultado = false;
//creo los objetos basados en ReflectionClass
$refObj1 = new ReflectionClass($obj1);
$refObj2 = new ReflectionClass($obj2);
//voy a comparar si ambos objetos tienen el mismo nombre de clase
if($refObj1->getName()==$refObj2->getName() )
{
//obtengo las propiedades de cada uno de los objetos
$aProp1=$refObj1->getProperties();
$aProp2=$refObj2->getProperties();
//voy a iterar entre todas las propiedades de los objetos
//como ya determine que ambos son de la misma clase
//ambos tienen la misma cantidad de propiedades
for($k=0;$k<sizeof($aProp2);$k++)
{
//si la propiedad es privada la coloco como accesible
if($aProp1[$k]->isPrivate())
{
$aProp1[$k]->setAccessible(true);
$aProp2[$k]->setAccessible(true);
}
//si la propiedad es protegida la coloco como accesible
if($aProp1[$k]->isProtected())
{
$aProp1[$k]->setAccessible(true);
$aProp2[$k]->setAccessible(true);
}
//comparo ambos valores
if($aProp1[$k]->getValue($obj1)==$aProp2[$k]->getValue($obj2))
$resultado = true;
else
{
//si una de las propiedades no es igual en ambos objetos
//termino el for
$resultado=false;
break 1;
}
}
}
else
$resultado = false;
return $resultado ;
}
}
?>

Cách dùng rất đơn giản:
<?php
include('compareobject.php');
class class1
{
private $x;
private $y;

function __set($propiedad,$valor)
{
$this->$propiedad=$valor;
}
function __get($propiedad)
{
return $this->$propiedad;
}
}

$object1=new class1();
$object1->x=10;
$object1->y=20;

$object2=new class1();
$object2->x=10;
$object2->y=20;

$object3=new class1();
$object3->x=10;
$object3->y=10;
if(compareobject::compareobjects($object1,$object2))
{
echo "The OBJECT 1: \n";
print_r($object1);
echo "\nIs Equal to \n"; //print 1
echo "The OBJECT 2: \n";
print_r($object2);
echo "\n";
echo "\n";
echo "\n";
}
if(!compareobject::compareobjects($object1,$object3))
{
echo "The OBJECT 1: \n";
print_r($object1);
echo "\nIs not equal to \n"; //print 1
echo "The OBJECT 3: \n";
print_r($object3);
}

?>

Chủ Nhật, 17 tháng 3, 2013

Livequery.js bổ sung cho jquery

Đây la 1 plugin của jquery được viết bởi Brandon Aaron voi chức năng sử dụng sức mạnh của bộ selector Jquery khởi tạo các sư kiện va gọi lại sau sự kiện cho cac yếu tố được đánh dấu một cách tự động ngay cả sau khi trang được load va cây DOM được updated ,yếu tố mới được tạo ra thông qua Ajax.
Các bạn download tại đây:https://github.com/brandonaaron/livequery/downloads
De de hieu hon chung ta cung di vao vi du cu the:

Ở ví dụ trên,chúng ta dùng jquery bình thường.Như các bạn thấy mỗi lần click vao thẻ a class="whatever" nó sẽ đưa ra cảnh báo alert('Olalalal').Tuy nhiên nếu bạn click vào thẻ a class="another" nó sẽ sinh thêm một thẻ a class="whatever",nhưng khi click vao thẻ a class="whatever" được sinh thêm này,nó không đưa ra cảnh báo alert('Olalalal');.Vậy các yếu tố DOM được updated ko chịu sự tác dụng của Jquery.
Nếu dùng cách khác:

Ở đây ta bọc sự kiện alert nằm trong sự kiên click có như vậy mới khởi động được sự kiện alert cho selector <a href="whee.html" class="whatever">Bork</a> mới tạo ra.Nhưng như thế thì alert chỉ xuất ra khi a.another được click.Quá phiền phức phải không các bạn,cách giải quyết đơn giản là sử dụng livequery():

Livequery sẽ tự động thêm sự kiện vào selector khi nó xuất hiện(được thêm vào).Vì thế bất kì a.whatever nào cho dù co từ trước hoặc thêm vào khi ta click vao đều đưa ra cảnh báo alert('Olalala')
Livequery co 3 cách dùng:

eventType:nhu click,hover.
eventHandler : function thuc thi khi su kien xay ra.

matchedFn:function duoc thuc thi khi yeu to moi duoc danh dau.

matchedFn:function duoc thuc thi khi yeu to moi duoc danh dau.
unmatchFn:function duoc thuc thi khi mot yeu to khong con duoc danh dau nua.
De stop livequery ta co 5 cach:

Cham dut livequery voi selector duoc chon.

Cham dut livequery voi selector duoc chon voi su kien eventType(giai thich o tren).

Cham dut livequery voi selector duoc chon voi su kien eventType va function dang thuc thi.

Cham dut livequery voi selector duoc chon voi function thuc thi khi yeu to moi duoc danh dau.

Cham dut livequery voi selector duoc chon voi cac function thuc thi khi yeu to moi duoc danh dau va khong con duoc danh dau.
Vi du:

Chúc các bạn học tập thật tốt với plugin tuyệt vời này.

Thứ Sáu, 15 tháng 3, 2013

Jquery :Tạo note với jquery

Hôm nay mình sẽ làm một cái note với jquery ,xem ra hình như nó chẳng có ứng dụng gì cả,chỉ là đẹp và là một cách thực tập với các hàm của jquery .

Các bạn có thể xem demo tại đây:

Thứ Hai, 11 tháng 3, 2013

Hàm apply() ,call() ,bind() và thuộc tính arguments,length,caller trong javascript

Đây là những hàm cũng ít người quan tâm tới ,tuy nhiên nếu nghiên cứu sâu nó sẽ giúp ích rất nhiều cho bạn đó:
Hàm call() dùng để gọi một method trong một object và các object thay thế cho object hiện hành ,với các tham số thông qua .
Cú pháp :
call([thisObj[, arg1[, arg2[, [, argN]]]]])
thisObj: Object được dùng như object hiện hành.Nếu để trống nó sẽ lấy Object global thay thế.
arg các tham số thông qua.

Một ví dụ dễ hiều :

Kết quả là đầu tiên nó sẽ đưa alert(10) bởi vì đối tượng thí trong function f() sẽ trỏ đối tượng global,lúc này x=10 sẽ được gọi ,sau đó sẽ alert(15) vì ta đã khởi động hàm f() với object o lúc này this sẽ trỏ tới o nên this.x=15.

_ Hàm apply()chức năng giống như hàm call() ,khác biệt là apply() yêu cầu tham số thứ hai phải là một mảng .
Một ví dụ dễ hiểu nhất:


Như bạn thấy đầu tiên nó sẽ gọi hàm g và thông qua các giá trị object,func,args.func lúc này sẽ là f1,giá trị thông qua sẽ là một mảng có 1 phần tử ["the value of x = "].Lúc này hàm f1 sẽ đựơc gọi thông qua apply() .Vì ta có thông qua o={x:15} là object nên tới function f1 ,this.x đươc hiểu như là object của o ,kết quả là :the value of x =15
Cũng tương tự vậy,chỉ khác là cách gọi thứ 2 thông qua mảng 2 phần tử,vậy f2 cần có 2 biến.Giá trị trả lại sẽ là: the value of x squared = 255 Wow!.
Hàm apply() trông có vẻ hiêu quả hơn trong nhiều trường hợp.


_Thuộc tính arguments dùng lấy tham số của object function thực thi nó :
Ví dụ:


Nó chỉ alert(H) vì hàm chỉ thông qua 1 tham số,nên nó sẽ lấy arguments[0]lúc này là H.
Tiếp theo:

Lúc này nó sẽ in ra Hello ,vì lúc đó mình nạo vét hết toàn bộ tham số đầu vào để nối chúng lại.

Thuộc tính length để xác định sô lương tham số thông qua một hàm :
Ví dụ:

Nó sẽ ra : Expected Arguments: 2
Passed Arguments: 2


Hàm bind() dùng để tạo hàm ràng buộc ,sao chép lại thân của hàm được gán.
Cú pháp:

function: là hàm cần sao chép.
thisArg là object sẽ gán tới this cho hàm sao chép .
arg : các tham số thông qua.
Ví dụ:

Kết quả là true.


Thuộc tính caller dùng để lấy tên hàm nào đã khởi động hàm này.
Ví dụ:

Nó sẽ in ra function writesub() { write(); } tức là sẽ in ra toàn bộ function đã khởi động nó.
Nếu caller đựơc đặt trong function hoạt động độc lập ,ko có hàm nào goi nó,thì nó đang ở top level of a JavaScript program ,caller lúc này sẽ trả lại null.
Ví dụ:

Nó sẽ in ra:CallLevel was called from the top level.

Chủ Nhật, 10 tháng 3, 2013

Code thần tốc với Texter


Với chức năng lưu các đoạn text code ,paste nhanh chóng những gì ta đã code trước đó.Nó là một phần mềm không thể thiếu với dân code.
Hãy download phần mềm :Texter
Chức năng có 2 dạng text và script:
_Với text:
<a href="%c">%|</a> :%c sẽ được thay thể bởi những gì mình đã copy vào trong clipboard.%| sẽ là nơi đặt trỏ chuột
%ds sẽ trả lại thời gian theo hình thức 3/9/2007.
%dl sẽ trả lại thời gian theo hình thức Friday, March 09, 2007.
%t sẽ trả lại thời gian theo hình thức 1:30 PM.
_Với script :
{Tab}, {Enter}, {Up}, {Down}, {Left}, {Right},{Backspace} sẽ giúp ta gõ các phím đó
# Windows key
! Alt key
^ Control key
+ Shift key
Ví dụ: +!{Down} thì nó sẽ gõ shift rồi đến alt rồi đến phím xuống.