Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. * Master-Detail - Exercise 2
  3. *
  4. * Def: A master–detail interface displays a master list and the details for the currently selected item. [Wikipedia]
  5. *
  6. * The goal of this exercise is to extend Master-Detail - Exercise 1 by adding user friendly interface via Node.js Express Server.
  7. */
  8.  
  9. /*
  10. * DN3.3:a) Dopolnite routes/index.js, tako da se seznam podrobnosti ne bo izpisan,
  11. * če uporabnik predhodno ne izbere elementa. Namig: pri klicu res.render()
  12. * nastavite atribut detail na vrednost null.
  13. *
  14. * Za polno število točk uporabite asinhroni rokovalnik napak
  15. * namesto vejitve namesto vejitve if/else. Glej: http://sequelizejs.com/docs/latest/misc
  16. *
  17. * b) Dopolnite routes/index.js in views/index.jade, tako da bo izbran element iz seznama master
  18. * označen z odebeljeno pisavo. Sicer naj bo izpisan z privzeto pisavo. Namig: v index.jade
  19. * uporabite vejitev in ustrezno oznako HTML.
  20. */
  21.  
  22. /*
  23. * DN3.4: Razširite nalogo DN3.3.
  24. *
  25. * Dopolnite routes/index.js, tako da bo prikazano dvo-nivojski vmesnik "master-detail".
  26. * Nivo 1 "master" je naj seznam dobaviteljev (CompanyName). Nivo 1 "detail" je naj seznam
  27. * produktov (ProductName) izbranega dobavitelja in hkrati tudi nivo 2 "master". Nivo 2 "detail"
  28. * so naj naročila (Quantity) izbranega produkta.
  29. *
  30. */
  31.  
  32. var Sequelize = require('sequelize');
  33. var sequelize = new Sequelize('northwind', 'nodejs', 'nodejs', {dialect: 'mysql',});
  34. var Project = require('sequelize-import')(__dirname + '/../models', sequelize, { exclude: ['index.js']});
  35.  
  36. Project.Suppliers.hasMany(Project.Products, {foreignKey: 'SupplierID'});
  37.  
  38. // 1. index handler is used when www client connects to localhost:3000 or localhost:3000/index [see app.js, line 35-36]
  39. // 2. [see app.js, line 35-36]
  40. exports.index = function(req, res){
  41.  
  42. // 3. Select an Item
  43. //
  44. // Client uses a hyperlink to select an item. A HTTP GET request is issued (e.g. localhost:3000/index?supplier=1)
  45. // The handler can access the supplier variable via server variable req.query.supplier
  46. var selectedItem = req.query.supplier || 1; // Defaults to 1 if req.query.supplier is undefined
  47. // ^----------------^
  48.  
  49. // 4. Execute Queries from the Master-Detail - Exercise 1
  50. Project.Suppliers
  51. .findAll()
  52. .success(function(qrm) {
  53.  
  54. if(qrm == null)
  55. throw "Err";
  56. //console.log(qrm);
  57.  
  58. Project.Suppliers
  59. .find({ where: { SupplierID: selectedItem }, include: [Project.Products,] })
  60. .success(function(qrd) {
  61.  
  62. if(qrd == null)
  63. throw "Err";
  64. //console.log(qrd);
  65.  
  66. // 5. Generate HTML page by invoking the Jade interpreter and providing the appropriate Jade template [see views/index.jade]
  67. // with two arguments: master and detail (lists)
  68. if (selectedItem == 1) {
  69. res.render('index', { title: 'Master-Detail', master: qrm, detail: null });
  70. } else {
  71.  
  72. res.render('index', { title: 'Master-Detail', master: qrm, detail: qrd.Products });
  73. // 6. [see views/index.jade]
  74. }
  75. })
  76. .error(function(err){
  77. render404(req, res);
  78. });
  79. })
  80. .error(function(err){
  81. render404(req, res);
  82. });
  83. };
  84.  
  85. render404 = function(req, res) {
  86. res.writeHead('Content-Type', 'text/plain');
  87. res.end("404");
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement