Created
December 5, 2012 22:23
-
-
Save shigeki/4220059 to your computer and use it in GitHub Desktop.
Object.observer() デモ2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Object.observer() demo2</title> | |
<style> | |
table, td, th { | |
border: 2px #000000 solid; | |
} | |
</style> | |
<script src="observer2.js"></script> | |
</head> | |
<body> | |
<div id="event"><div>Event Code</div> | |
<pre> | |
var first_time = obj2.seen; // 時間参照イベント | |
obj2.seen = new Date(); // 時間更新イベント | |
var second_time = obj2.seen; // 時間参照イベント | |
</pre> | |
</div> | |
<div id="seen"></div> | |
<div id="results"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener('DOMContentLoaded',function() { | |
if (!Object.observe) { | |
alert('Object.observe not supported in your browser. Please use Chrome Dev Channel and enable Experimental JavaScript'); | |
return; | |
} | |
var obj2 = {_time: new Date(0)}; | |
var notifier = Object.getNotifier(obj2); | |
Object.defineProperties(obj2, { | |
_time: { | |
enumerable: false, | |
configrable: false | |
}, | |
seen: { | |
set: function(val) { | |
var notifier = Object.getNotifier(this); | |
notifier.notify({ | |
type: 'time_updated', // 時間更新イベントの定義 | |
name: 'seen', | |
oldValue: this._time | |
}); | |
this._time = val; | |
}, | |
get: function() { | |
var notifier = Object.getNotifier(this); | |
notifier.notify({ | |
type: 'time_read', // 時間参照イベントの定義 | |
name: 'seen', | |
oldValue: this._time | |
}); | |
return this._time; | |
} | |
} | |
}); | |
Object.observe(obj2, output); | |
var seen = document.getElementById('seen'); | |
var first_seen = document.createElement('div'); | |
var first_time = obj2.seen; // 時間参照イベント | |
first_seen.innerText = 'first_seen:' + first_time; | |
seen.appendChild(first_seen); | |
obj2.seen = new Date(); // 時間更新イベント | |
var second_seen = document.createElement('div'); | |
var second_time = obj2.seen; // 時間参照イベント | |
second_seen.innerText = 'second_seen:' + second_time; | |
seen.appendChild(second_seen); | |
function output(changes) { | |
var results = document.getElementById('results'); | |
var table = document.createElement('table'); | |
results.appendChild(table); | |
var caption = document.createElement('caption'); | |
caption.innerText = 'Observed Events List'; | |
table.appendChild(caption); | |
var thead = document.createElement('thead'); | |
thead.innerHTML = '<tr><th>index</th><th>type</th><th>name</th><th>oldValue</th></tr>'; | |
table.appendChild(thead); | |
changes.forEach(function(change, i) { | |
var tr = document.createElement('tr'); | |
tr.innerHTML = '<td>' + i + '</td><td>' + change.type + '</td><td>' + change.name + '</td><td>' + change.oldValue + '</td>'; | |
table.appendChild(tr); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment