Skip to content

Commit

Permalink
first try one class
Browse files Browse the repository at this point in the history
  • Loading branch information
pofigizm committed Jun 15, 2014
1 parent a8b066f commit f52c230
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
6 changes: 5 additions & 1 deletion index.html
@@ -1,5 +1,8 @@
<!doctype html>
<html>
<head>
<script src='order.js'></script>
</head>
<body>
<a href='#' onclick='getobject()'>getobject</a>
<script>
Expand All @@ -11,7 +14,8 @@
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;

var myobject = JSON.parse(this.responseText);
var xhrobj = JSON.parse(this.responseText);
var myobject = new Order(xhrobj);

console.log(myobject);
console.log(myobject.summ());
Expand Down
39 changes: 39 additions & 0 deletions order.js
@@ -0,0 +1,39 @@
(function (factory){
'use strict';

if( typeof define === 'function' && define.amd ){
define(factory);
} else if( typeof module != 'undefined' &&
typeof module.exports != 'undefined' ){
module.exports = factory();
} else {
window['Order'] = factory();
}
})(function (){
'use strict';

// Class definition

function Order(object) {
object.parts = object.parts || [];
var partsArr = [];
object.parts.forEach(function (value, key) {
partsArr[key] = new Order(value);
});

this.name = object.name || 'Default';
this.cost = object.cost || 0;
this.parts = partsArr;
}

Order.prototype.summ = function() {
var summ = 0;
this.parts.forEach(function(part) {
summ += part.summ();
});
return summ + this.cost;
};

// Export
return Order;
});
31 changes: 11 additions & 20 deletions server.js
@@ -1,39 +1,30 @@
var http = require('http');
var fs = require('fs');
var url = require('url');
var Order = require('./order')

function ClassOrder(name, cost) {
this.name = name;
this.cost = cost;
this.parts = [];
this.summ = function() {
var summ = 0;
this.parts.forEach(function(part) {
summ += part.summ();
});
return summ + this.cost;
};
}

var myobject = new ClassOrder('my object', 10);
myobject.parts[0] = new ClassOrder('my first part', 20);
myobject.parts[1] = new ClassOrder('my second part', 30);
myobject.parts[1].parts[0] = new ClassOrder('my first in second part', 40);
var myobject = new Order({name:'my object', cost: 10});
myobject.parts[0] = new Order({name:'my first part', cost: 20});
myobject.parts[1] = new Order({name:'my second part', cost: 30});
myobject.parts[1].parts[0] = new Order({name:'my first in second part', cost: 40});

console.log(myobject);
console.log(myobject.summ());

http.createServer(function(req, res) {
switch(req.url) {
case '/':
sendFile("index.html", res);
sendFile('index.html', res);
break;
case '/order.js':
sendFile('order.js', res);
break;
case '/getobject':
res.end(JSON.stringify(myobject));
break;
default:
res.statusCode = 404;
res.end("Page not found!");
res.end('Page not found!');
}
}).listen(80);

Expand All @@ -42,7 +33,7 @@ function sendFile(fileName, res) {
fileStream
.on('error', function () {
res.statusCode = 500;
res.end("Server error");
res.end('Server error');
})
.pipe(res)
.on('close', function () {
Expand Down

0 comments on commit f52c230

Please sign in to comment.