From 52f0120f3978a5e237004a4f197407804a8b683b Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 7 Jan 2014 18:12:37 -0300 Subject: [PATCH] get block in angular from node! --- app/controllers/blocks.js | 10 ++++------ app/controllers/index.js | 22 ++++++++++++++++++++++ app/models/Block.js | 2 +- app/views/includes/foot.jade | 1 + app/views/index.jade | 21 --------------------- config/routes.js | 2 +- public/js/app.js | 5 +++-- public/js/controllers/index.js | 9 +++++++-- public/js/services/index.js | 5 +++++ public/views/index.html | 7 ++++++- 10 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 public/js/services/index.js diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index fdef665..39b508e 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -1,15 +1,13 @@ 'use strict'; - -var Block = require('../models/Block'); -//, _ = require('lodash'); - - - /** * Module dependencies. */ +var mongoose = require('mongoose'), + Block = mongoose.model('Block'); +//, _ = require('lodash'); + /** * Find block by hash ... diff --git a/app/controllers/index.js b/app/controllers/index.js index f71d55c..423b411 100644 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -1,5 +1,27 @@ 'use strict'; +/** + * Module dependencies. + */ + +var mongoose = require('mongoose'), + Block = mongoose.model('Block'); + exports.render = function(req, res) { res.render('index'); }; + +/** + * List of blocks at HomePage + */ +exports.all = function(req, res) { + Block.find().limit(7).exec(function(err, blocks) { + if (err) { + res.render('error', { + status: 500 + }); + } else { + res.jsonp(blocks); + } + }); +}; diff --git a/app/models/Block.js b/app/models/Block.js index 53272b5..de89280 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -65,4 +65,4 @@ BlockSchema.statics.fromHash = function(hash, cb) { }).exec(cb); }; -module.exports = mongoose.model('Block', BlockSchema); +mongoose.model('Block', BlockSchema); diff --git a/app/views/includes/foot.jade b/app/views/includes/foot.jade index f7cc3e6..e4c8706 100755 --- a/app/views/includes/foot.jade +++ b/app/views/includes/foot.jade @@ -24,6 +24,7 @@ script(type='text/javascript', src='/js/filters.js') //Application Services script(type='text/javascript', src='/js/services/global.js') +script(type='text/javascript', src='/js/services/index.js') //Application Controllers script(type='text/javascript', src='/js/controllers/index.js') diff --git a/app/views/index.jade b/app/views/index.jade index 7f33718..a74f173 100755 --- a/app/views/index.jade +++ b/app/views/index.jade @@ -2,24 +2,3 @@ extends layouts/default block content section.container(data-ng-view) - - section.container - p ˈmɪst(ə)ri/' - | noun - audio(src="https://ssl.gstatic.com/dictionary/static/sounds/de/0/mystery.mp3",preload="auto",data-dobid="aud",id="aud") - button(onclick="document.getElementById('aud').play()") Play - - ol - li - strong something that is difficult or impossible to understand or explain. - p "the mysteries of outer space" - | synonyms: puzzle, enigma, conundrum, riddle, secret, unsolved problem, problem, question, question mark, closed book; secrecy or obscurity. - p "much of her past is shrouded in mystery" - | synonyms: secrecy, darkness, obscurity, ambiguity, ambiguousness, uncertainty, impenetrability, vagueness, nebulousness; More - li - strong a person or thing whose identity or nature is puzzling or unknown. - p "‘He's a bit of a mystery,’ said Nina" - li - strong a novel, play, or film dealing with a puzzling crime, especially a murder. - p "the 1920s murder mystery, The Ghost Train" - | synonyms: thriller, detective story/novel, murder story; More diff --git a/config/routes.js b/config/routes.js index 7150af0..3da0264 100644 --- a/config/routes.js +++ b/config/routes.js @@ -5,12 +5,12 @@ module.exports = function(app) { //Home route var index = require('../app/controllers/index'); app.get('/', index.render); + app.get('/last_blocks', index.all); //Block routes var blocks = require('../app/controllers/blocks'); app.get('/block/:blockHash', blocks.show); - app.param('blockHash', blocks.block); diff --git a/public/js/app.js b/public/js/app.js index 77b64cb..afb98d4 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,5 +1,6 @@ 'use strict'; -angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system']); +angular.module('mystery', ['ngCookies', 'ngResource', 'ngRoute', 'ui.bootstrap', 'ui.route', 'mystery.system', 'mystery.index']); -angular.module('mystery.system', []); \ No newline at end of file +angular.module('mystery.system', []); +angular.module('mystery.index', []); diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index 4cb2b6f..bec0f0a 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -1,5 +1,10 @@ 'use strict'; -angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', function ($scope, Global) { +angular.module('mystery.system').controller('IndexController', ['$scope', 'Global', 'Index', function ($scope, Global, Index) { $scope.global = Global; -}]); \ No newline at end of file + $scope.last_blocks = function() { + Index.query(function(blocks) { + $scope.blocks = blocks; + }); + }; +}]); diff --git a/public/js/services/index.js b/public/js/services/index.js new file mode 100644 index 0000000..f961642 --- /dev/null +++ b/public/js/services/index.js @@ -0,0 +1,5 @@ +'use strict'; + +angular.module('mystery.index').factory('Index', ['$resource', function($resource) { + return $resource('/last_blocks'); +}]); diff --git a/public/views/index.html b/public/views/index.html index b92fe02..d84010f 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -1,5 +1,10 @@ -
+
+
    +
  • + {{block.hash}} +
  • +