Last active
March 29, 2020 04:48
-
-
Save wtnabe/361f8772adcf78d625d273afb5544e30 to your computer and use it in GitHub Desktop.
jQuery and MobX counter
This file contains 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
<html> | |
<head> | |
<script crossorigin type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mobx/4.15.0/mobx.umd.min.js"></script> | |
<script crossorigin type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script> | |
<script type="text/javascript"> | |
controller = { | |
$el: undefined, | |
init: function ($el) { | |
this.$el = $el | |
this.render() | |
var self = this | |
this.$el.on('up', function () { | |
self.state.upCount() | |
}) | |
mobx.autorun(function () { self.render() }) | |
}, | |
state: mobx.observable( | |
{ | |
name: 'counter', | |
count: 0, | |
upCount: function () { | |
this.count++ | |
} | |
}, | |
{ | |
upCount: mobx.action | |
}), | |
render: function () { | |
this.$el.find('#value').text(this.state.count) | |
} | |
} | |
$(function() { | |
controller.init($('#counter')) | |
}) | |
</script> | |
<title>jQuery + MobX counter</title> | |
</head> | |
<body> | |
<h1>jQuery + MobX counter</h1> | |
<div id="counter"><!-- container --> | |
<p id="value"></p> | |
<button onclick="$(this).trigger('up')">up</button> | |
</div><!-- end of container --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment