Skip to content

Instantly share code, notes, and snippets.

@niftydevelopment
Created December 18, 2013 23:27
Show Gist options
  • Select an option

  • Save niftydevelopment/8031667 to your computer and use it in GitHub Desktop.

Select an option

Save niftydevelopment/8031667 to your computer and use it in GitHub Desktop.
Koden från min föreläsning på Jönköping Developer Dojo
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
document.write("\<script src='resources/jQuery.js' type='text/javascript'>\<\/script>");
</script>
<script type="text/javascript">
//Följande modul kan man ladda ner till sin data här: https://gist.github.com/fatihacet/1290216
document.write("\<script src='resources/pubsub.js' type='text/javascript'>\<\/script>");
</script>
<script>
//Singleton
var singleton = {}
//Name spacing using object literals (java like)
var myApp = {
domain : {
entity : {},
valueObject : {},
service: {}
},
util: {
DOM : {}
}
}
console.debug(myApp);
//Constructor (java like)
//basic
//private variables
//private functions
//public functions
myApp.domain.entity.Car = function(model, milageage) {
this.model = model;
this.mileage = milageage;
var cost = 400000;
function calculatePriceBasedOnMileage() {
return cost = milageage * myApp.domain.entity.Car.PRICELOSSPERMILE;
}
this.getCost = function() {
return calculatePriceBasedOnMileage();
}
}
//static varables
myApp.domain.entity.Car.PRICELOSSPERMILE = 50;
//var car = new myApp.domain.entity.Car("Toyata", 1000);
//console.debug(car.getCost());
//constructor with prototype
myApp.domain.entity.Car.prototype.toString = function() {
return "myApp.domain.entity.Car / Model: " + this.model + " Mileage: " + this.mileage;
}
//console.debug(car.toString());
//IIFE (Immediately-Invoked Function Expression)
//Varför?
//initierar sig själv.
//dra nytta av function isolation.
var x = function() {
console.debug("hejsan");
};
(function() {
console.debug("do stuff");
})();
//Modules
//A service
//(utöka namespace)
myApp.domain.service.rentService =
(function(pubsub) {
var availableCars = 10;
function rentACar(car) {
--availableCars;
pubsub.publish("acarisrented", car);
}
function checkAvailability() {
return availableCars;
}
return {
rent : rentACar,
checkAvailability : checkAvailability
}
})(pubsub);
(function() {
var service = myApp.domain.service.rentService;
console.debug(service.checkAvailability());
service.rent();
service.rent();
service.rent();
console.debug(service.checkAvailability());
})();
//A dom appender
//(utöka namespace)
myApp.util.DOM.rentedCarsWriter =
(function(jq, pubsub) {
var nodeToWriteTo;
pubsub.subscribe("acarisrented", writeCarDetailsToNode);
function writeCarDetailsToNode(topic, car) {
if (nodeToWriteTo == null || !nodeToWriteTo.append) {
nodeToWriteTo = jq("#carJustRented");
}
console.debug(nodeToWriteTo);
nodeToWriteTo.text(nodeToWriteTo.text() + car);
}
return {
write : writeCarDetailsToNode
}
})($, pubsub);
//connect the two :)
function rent() {
var e = myApp.domain.entity;
var service = myApp.domain.service.rentService;
service.rent(new e.Car("BMW", 120));
}
</script>
<title></title>
</head>
<body>
<button type="button" onclick="rent()">Rent a Toyota</button>
-
Car just rented:
<div id="carJustRented"></div>
-
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment