`
DavyJones2010
  • 浏览: 154273 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Javascript: Event Binding Model Introduction

阅读更多

1. Two ways to bind a method to an event.

    1. Using tag binding:

<html>
	<head>
		<script type="text/javascript">
		function display(){
			var str = "hello world";
			alert(str);
		}
		</script>
	</head>
	<body onmousedown="display();">
	</body>
</html>

    2. Using callback function assignment:

<html>
	<head>
		<script type="text/javascript">
		function display(){
			var str = "hello world";
			alert(str);
		}
		document.onmousedown = display;
		</script>
	</head>
	<body>
	</body>
</html>

    Comments;

    1. The first approach is more flexiable cause we can assign callbacks dynamically. 

    2. The second approach is more intuitive compared with the first.

 

Example:

   1. Works fine.

<html>
	<head>
		<script type="text/javascript">
		function display(){
			var str = "hello world";
			alert(str);
		}
		</script>
	</head>
	<body>
		<button onclick="display();" value="CLICK ME" />
	</body>
</html>

   2. Cannot find the object cause the html parse procedure is from front to end.

<html>
	<head>
		<script type="text/javascript">
		// function display(){
		// 	var str = "hello world";
		// 	alert(str);
		// }
		var btn = document.getElementById("button");
		alert(btn);
		</script>
	</head>
	<body>
		<input type="button" id="button" value="click me" />
	</body>
</html>
// Would alert NULL.
// Cause when we execute the script, the button hasn't been initialized yet.
// If we move the scriptlet to the end of <body> tag, it works fine.

   3. A better solution for loading null.

<html>
	<head>
		<script type="text/javascript">
		function display(){
			var str = "hello world";
			alert(str);
		}
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = display;  //Callbacks binding.
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>

      Below is using a anonymous function.

<html>
	<head>
		<script type="text/javascript">
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = function(){
				var str = "hello world";
				alert(str);
			};
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>

    

P.S.

   1. For event handler, an event object will be passed into it.

       For IE/Firefox, code below will both works fine. Cause there will be an inner event object named 'event' passed into the callbacks.

<html>
	<head>
		<script type="text/javascript">
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = display;
		}
		function display(){
			var str = "hello world";
			alert(event.type + " " + str)
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>

   2. For Firefox, code below works fine but it is not the case when using IE

<html>
	<head>
		<script type="text/javascript">
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = display;
		}
		function display(event){
			var str = "hello world";
			alert(event.type + " " + str)
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>
<html>
	<head>
		<script type="text/javascript">
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = display;
		}
		function display(event1){
			var str = "hello world";
			alert(event1.type + " " + str)
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>
// click hello world

   So we have to take the different implementation of JS into account when developing multi-browser app.

   In IE, "event" is a pre-defined object like that of "document" that is instantiated by SYSTEM/Browser automatically.

   In FIREFOX, an event object will be instantionlized and passed into the callback function.

   3. BTW, what if we want to pass params into callbacks?

       How can we handle the existance of "event" and self-defined param?

<html>
	<head>
		<script type="text/javascript">
		function init(){
			var btn = document.getElementById("button");
			btn.onclick = display;
		}
		function display(){
			var str = "hello world";
			alert(this.value + " " + str);
		}
		</script>
	</head>
	<body onload="init();">
		<input type="button" id="button" value="click me" />
	</body>
</html>

    Comments: Here, "this" represents the object that called this function that is the button.

    Result would be : "click me hello world".

    So we don't have to pass param into the callbacks, cause it can get everything, every param it needs.

    If it need get param that inbedded in button, just use key word "this".

    If it need get param that of other controls, just use "document.get***By***();".

分享到:
评论

相关推荐

    EFI Howto:Driver binding的实质

    EFI驱动模型与Windows驱动模型的比较和Driver Binding的实现 本文主要介绍了EFI驱动模型的概念,特别是Driver Binding的实现过程,同时借鉴了Windows驱动模型的一些概念,以便更好地理解EFI驱动模型。 首先,我们...

    Android代码-AndroidMVVM框架写在kotlin中

    TwoWay: Binding from model to view and view to model OneWayToSource: Binding from view to model OneTime: Binding from model to view, and auto release after first emit Simple Binding verticalLayout { ...

    Event Binding Markup

    在WPF(Windows Presentation Foundation)开发中,Event Binding Markup是一种重要的技术,用于将事件与视图模型中的命令关联,从而实现MVVM(Model-View-ViewModel)设计模式的有效利用。Event Binding Markup ...

    WPF纯MVVM事件绑定+事件参数 完美示例

    &lt;i:EventTrigger EventName="KeyDown"&gt; &lt;i:InvokeCommandAction Command="{Binding KeyDownCommand}" /&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Behaviors&gt; ``` 这里的`i:`前缀是指向`System.Windows....

    Android Data Binding

    implementation 'androidx.databinding:viewbinding:4.0.1' ``` 3. **生成绑定类**:编译时,Data Binding会自动生成一个绑定类,用于在Java代码中访问和操作布局中的元素。 4. **数据绑定表达式**:例如,`@{...

    Windows Presentation Foundation 4.5 Cookbook的源码

    Introduction 7 Creating custom type instances in XAML 9 Creating a dependency property 15 Using an attached property 25 Creating an attached property 28 Accessing a static property from XAML 33 ...

    JGoodies Binding

    2. **MVC模式实现**:在基于Model-View-Controller模式的项目中,JGoodies Binding可以作为连接模型和视图的关键组件。 3. **快速原型构建**:对于需要快速搭建UI原型的项目,JGoodies Binding的便捷性和自动化特性...

    jgoodies-binding-2_6

    5. **ValueModel**:这是JGoodies Binding的核心概念之一,它封装了一个可以观察和修改的值。ValueModel可以是简单的Java对象,也可以是复杂的属性结构,通过ValueModel可以方便地实现数据绑定。 6. **Collections ...

    自定义android开发MVP+ViewBinding架构Demo

    在Android应用开发中,MVP(Model-View-Presenter)架构模式是一种常用的设计模式,它有助于保持代码的清晰和可维护性。ViewBinding是Google官方推荐的一种替代findViewById的方法,可以更方便地处理UI元素和业务...

    领域驱动设计(精简版)

    Part I Putting the Domain Model to Work 1 Chapter 1: Crunching Knowledge 7 Chapter 2: Communication and the Use of Language 23 Chapter 3: Binding Model and Implementation 45 Part II Chapter 4: ...

    node-sass_win&linux_x64-72_binding.node.zip

    Cannot download linux-x64-72_binding.node/win32-x64-72_binding.node

    LearningAndroidDataBinding:体验Data Binding库的用法

    该类是model和view数值连接的桥梁。 该类的命名规则是基于layout文件名称首字母大写并连接“Binding”后缀产生,例如: activity_getting_started.xml =&gt; ActivityGettingStartedBinding 3.activity与layout的连接...

    WPF ComboBox Command Binding

    `Command Binding`是WPF中实现这一目标的一种优雅方式,它将视图(View)和视图模型(ViewModel)连接起来,遵循MVVM(Model-View-ViewModel)设计模式。 MVVM模式鼓励将业务逻辑和UI逻辑分离,`Command`对象承载了...

    C# MVVM Binding demo

    在本文中,我们将深入探讨C#中的MVVM(Model-View-ViewModel)绑定技术,并通过一个名为"BookManage"的示例项目进行演示。MVVM是一种设计模式,它在WPF(Windows Presentation Foundation)和UWP(Universal Windows...

    DataBinding使用详解(一).pdf

    * Data Binding:Data Binding 是一种数据绑定技术,允许开发者在布局文件中使用绑定表达式来关联变量和控件。 * 绑定表达式:绑定表达式是使用“@{}”语法定义的,用于关联变量和控件。 * 变量:变量是指在布局文件...

    通过BindingSource实现DataSet、DataGridView和TextBox的数据同步更新.docx

    `DataSetManager().UpdateDataSet(dataSetModels, typeof(ModelData));` `MessageBox.Show("保存成功!");` `}` 通过使用 BindingSource,我们可以轻松地实现 DataSet、DataGridView 和 TextBox 的数据同步更新,...

    OSEK_VDX_Network_Management_Concept_and_API(NM)_V2.5.3_2004.pdf

    -OSEK Glossary (位于OSEK Binding 1.4.1,ISO 17356-1的一部分,由ISO-style introduction和glossary组成) -OSEK Binding Specification(基础:OSEK Binding 1.4.1,ISO 17356-2,glossary除外) -OSEK OS(基础...

    OSEK_VDX_Network_Management_Concept_and_API(NM)_V2.5.2_2003.pdf

    -OSEK Glossary (位于OSEK Binding 1.4.1,ISO 17356-1的一部分,由ISO-style introduction和glossary组成) -OSEK Binding Specification(基础:OSEK Binding 1.4.1,ISO 17356-2,glossary除外) -OSEK OS(基础...

    wpf Binding class

    public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ...

Global site tag (gtag.js) - Google Analytics