之前的章節許多示例代碼也或多或少地展示了如何使用ms-click來綁定事件了。能直接在模板上綁定是事件,這也是靜態模板與動態綁定的一大區別。ms-click不是簡單的onclick的別名,它在內部屏蔽了瀏覽器的差異,並且對許多瀏覽器暫時不支持的事件做了兼容處理。
總的來說,事件綁定是使用ms-on-☆綁定來實現,但avalon也提供了許多快捷方式,讓用戶能直接以ms-eventName調用那些常用事件,如下
animationend、 blur、 change、 input、 click、 dblclick、 focus、 keydown、 keypress、 keyup、 mousedown、 mouseenter、 mouseleave、 mousemove、 mouseout、 mouseover、 mouseup、 scan、 scroll、 submit
事件綁定的屬性值的格式,必須是函數名或函數名后+小括號(小括號里面添加參數)。
avalon的事件綁定支持多投事件機制(同一個元素可以綁定N個同種事件,如ms-click=fn, ms-click-1=fn2, ms-click-2=fn3),支持傳參(默認第一個參數為事件對象,如果第一個位置被占了,我們可以在其他位置使用$event引用事件對象。)
<!DOCTYPE HTML>
<html>
<head>
<title>ms-on</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="avalon.js" ></script>
<script>
var model = avalon.define({
$id: "test",
firstName: "司徒",
array: ["aaa", "bbb", "ccc"],
argsClick: function(e, a, b) {
alert([].slice.call(arguments).join(" "))
},
loopClick: function(a, e) {
alert(a + " " + e.type)
},
status: "",
callback: function(e) {
model.status = e.type
},
field: "",
check: function(e) {
model.field = this.value + " " + e.type
},
submit: function() {
var data = model.$model
if (window.JSON) {
setTimeout(function() {
alert(JSON.stringify(data))
})
}
}
})
</script>
</head>
<body>
<fieldset ms-controller="test">
<legend>有關事件回調傳參</legend>
<div ms-mouseenter="callback" ms-mouseleave="callback">{{status}}<br/>
<input ms-on-input="check"/>{{field}}
</div>
<div ms-click="argsClick($event, 100, firstName)">點我</div>
<div ms-each-el="array" >
<p ms-click="loopClick(el, $event)">{{el}}</p>
</div>
<button ms-click="submit">點我</button>
</fieldset>
</body>
</html>

<!DOCTYPE HTML>
<html>
<head>
<title>ms-on</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="avalon.js" ></script>
<script>
var count = 0
var model = avalon.define({
$id: "multi-click",
str1: "1",
str2: "2",
str3: "3",
click0: function() {
model.str1 = "xxxxxxxxx" + (count++)
},
click1: function() {
model.str2 = "xxxxxxxxx" + (count++)
},
click2: function() {
model.str3 = "xxxxxxxxx" + (count++)
}
})
</script>
</head>
<body>
<fieldset>
<legend>一個元素綁定多個同種事件的回調</legend>
<div ms-controller="multi-click">
<div ms-click="click0" ms-click-1="click1" ms-click-2="click2" >請點我</div>
<div>{{str1}}</div>
<div>{{str2}}</div>
<div>{{str3}}</div>
</div>
</fieldset>
</body>
</html>

<!DOCTYPE HTML>
<html>
<head>
<title>ms-on</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="avalon.js" ></script>
<script>
avalon.define({
$id: "xxx",
fn: function() {
console.log("11111111")
},
fn1: function() {
console.log("2222222")
},
fn2: function() {
console.log("3333333")
}
})
</script>
</head>
<body>
<div ms-controller="xxx"
ms-on-mouseenter-3="fn"
ms-on-mouseenter-2="fn1"
ms-on-mouseenter-1="fn2"
style="width:100px;height:100px;background: red;"
>
</div>
</body>
</html>

avalon已經對ms-mouseenter, ms-mouseleave進行修復,可以在這里與這里了解這兩個事件。到chrome30時,所有瀏覽器都原生支持這兩個事件。
<!DOCTYPE html> <html>
<head>
<title>ms-mouseenter, ms-mouseleave</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="avalon.js"></script>
<script>
avalon.define({
$id: "test",
fn1: function(e) {
console.log(e.type)
console.log(this)
},
fn2: function(e) {
console.log(e.type)
console.log(this)
}
})
</script>
</head>
<body ms-controller="test">
<div ms-mouseenter="fn1" ms-mouseleave="fn2" style="background: red;width:200px;height: 200px;padding:20px;">
<div style="background: blue;width:160px;height: 160px;margin:20px;"></div>
</div>
</body> </html>

最后是mousewheel事件的修改,主要問題是出現firefox上,它死活也不願意支持mousewheel,在avalon里是用DOMMouseScroll或wheel實現模擬的。我們在事件對象通過wheelDelta屬性是否為正數判定它在向上滾動。
<!DOCTYPE html>
<html>
<head>
<title>ms-on-mousewheel</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="avalon.js"></script>
<script>
var model = avalon.define({
$id: "test",
text: "",
callback: function(e) {
model.text = e.wheelDelta + " " + e.type
}
})
</script>
</head>
<body ms-controller="test">
<div ms-on-mousewheel="callback" id="aaa" style="background: red;width:200px;height: 200px;">
{{text}}
</div>
</body>
</html>

此外avalon還對input,animationend事件進行修復,大家也可以直接用avalon.bind, avalon.fn.bind來綁定這些事件。但建議都用ms-on綁定來處理。
