Skip to content

Instantly share code, notes, and snippets.

@mitsuruog
Created March 1, 2015 14:08
Show Gist options
  • Save mitsuruog/d520426ec33ed1880a4c to your computer and use it in GitHub Desktop.
Save mitsuruog/d520426ec33ed1880a4c to your computer and use it in GitHub Desktop.
JS Bin knockout.jsでFRPサンプル1 // source http://jsbin.com/fupacu
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="knockout.jsでFRPサンプル1">
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.24/rx.all.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input type="email" name="email" id="email">
<div class="results"></div>
<script id="jsbin-javascript">
function ViewModel() {
this.email = observable();
}
// observer
// 指定の値を観測して、変化があったらNotifyを送る
function observable() {
var value;
var module = function(newValue) {
if(typeof(newValue) === 'undefined') {
// getter
return value;
} else {
// setter
value = newValue;
module.valueChanged();
}
};
module.valueChanged = function() {
// subscriberに通知
module.subscriber(value);
};
return module;
}
// ViewModelとDOMをバインドする
function bindViewModel(vm) {
// vmのemailがpropertyNameとして受け取れる
var propertyNames = Object.getOwnPropertyNames(vm);
propertyNames.forEach(function(propertyName) {
// viewというか観測対象のcomponentかな。。。
var view = document.getElementById(propertyNames);
var property = vm[propertyNames];
// ViewModel -> View
property.subscriber = function(newValue) {
view.value = newValue;
};
// View -> ViewModel
view.addEventListener('input', function(e) {
property(e.target.value);
});
});
}
var vm = new ViewModel();
bindViewModel(vm);
vm.email('[email protected]');
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="knockout.jsでFRPサンプル1">
<script src="//cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.24/rx.all.js"><\/script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<input type="email" name="email" id="email">
<div class="results"></div>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">function ViewModel() {
this.email = observable();
}
// observer
// 指定の値を観測して、変化があったらNotifyを送る
function observable() {
var value;
var module = function(newValue) {
if(typeof(newValue) === 'undefined') {
// getter
return value;
} else {
// setter
value = newValue;
module.valueChanged();
}
};
module.valueChanged = function() {
// subscriberに通知
module.subscriber(value);
};
return module;
}
// ViewModelとDOMをバインドする
function bindViewModel(vm) {
// vmのemailがpropertyNameとして受け取れる
var propertyNames = Object.getOwnPropertyNames(vm);
propertyNames.forEach(function(propertyName) {
// viewというか観測対象のcomponentかな。。。
var view = document.getElementById(propertyNames);
var property = vm[propertyNames];
// ViewModel -> View
property.subscriber = function(newValue) {
view.value = newValue;
};
// View -> ViewModel
view.addEventListener('input', function(e) {
property(e.target.value);
});
});
}
var vm = new ViewModel();
bindViewModel(vm);
vm.email('[email protected]');</script></body>
</html>
function ViewModel() {
this.email = observable();
}
// observer
// 指定の値を観測して、変化があったらNotifyを送る
function observable() {
var value;
var module = function(newValue) {
if(typeof(newValue) === 'undefined') {
// getter
return value;
} else {
// setter
value = newValue;
module.valueChanged();
}
};
module.valueChanged = function() {
// subscriberに通知
module.subscriber(value);
};
return module;
}
// ViewModelとDOMをバインドする
function bindViewModel(vm) {
// vmのemailがpropertyNameとして受け取れる
var propertyNames = Object.getOwnPropertyNames(vm);
propertyNames.forEach(function(propertyName) {
// viewというか観測対象のcomponentかな。。。
var view = document.getElementById(propertyNames);
var property = vm[propertyNames];
// ViewModel -> View
property.subscriber = function(newValue) {
view.value = newValue;
};
// View -> ViewModel
view.addEventListener('input', function(e) {
property(e.target.value);
});
});
}
var vm = new ViewModel();
bindViewModel(vm);
vm.email('[email protected]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment