Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created September 7, 2015 14:17
Show Gist options
  • Select an option

  • Save MagnusThor/a5ae4ca760bc9ac8eaba to your computer and use it in GitHub Desktop.

Select an option

Save MagnusThor/a5ae4ca760bc9ac8eaba to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Custom Geo Binder</title>
</head>
<body>
<div id="app">
<ul>
<li data-repeat="locations">
<span data-bind="text:caption,location:$this">
</span>
</li>
</ul>
</div>
<script src="/src/bob.core.latest.js"></script>
<script>
var Location = (function () {
var model = function (caption, address, lat, lng) {
this.caption = caption;
this.address = address;
this.lat = lat;
this.lng = lng;
};
return model;
})();
var ViewModel = (function () {
var model = function () {
this.locations = [];
};
return model;
})();
var vm = new ViewModel();
Bob.binders.registerBinder("location", function (node, onchange, onadd, onremove) {
var serialize = function(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
};
node.addEventListener('mouseover', function () {
var params = serialize(node.dataset);
// not using the params
var await = fetch("/foo/index", {
headers: {
'method' : 'get',
'accept': 'application/json',
'content-type': 'application/json'
}
});
await.then(function(result) {
return result.text();
}).then(function(json) {
console.log(json);
}).catch(function(err) {
console.log("err", err);
});
});
return {
updateProperty: function (value) {
Object.keys(value).forEach(function(key) {
node.dataset[key] = value[key];
});
}
};
});
$(function () {
console.log("Bob Ready..");
vm.locations.push(new Location("Sundsvall", "Sundsvall, Sweden", "63", "17"));
Bob.apply(Bob.binders).bind($("#app"), vm);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment