Permalink
Browse files

Started to work on the router.

  • Loading branch information...
xcambar committed Aug 29, 2011
1 parent 812aa6b commit 2ecb4c417ee04a9ddbcbd5428552e1cea359345c
Showing with 263 additions and 156 deletions.
  1. +1 −0 .gitignore
  2. +39 −0 config/routes.json
  3. +0 −138 examples/CRUD.js
  4. +32 −0 examples/CRUD/app.js
  5. +32 −0 examples/CRUD/config/routes.json
  6. +108 −0 examples/CRUD/lib/MyCRUD/route/doc.js
  7. +38 −0 lib/Next/router.js
  8. +13 −18 tests/testNExt.js
View
@@ -1 +1,2 @@
lib/Ext
+lib-cov
View
@@ -0,0 +1,39 @@
+/**
+ * Definition of the routes used by the app
+ *
+ **/
+
+/**
+exports.routes = [
+ {
+ route: '/doc/:start?/:limit?',
+ method: 'GET',
+ handlerCls: 'Next.route.doc'
+ },
+ {
+ route: '/doc/count',
+ method: 'GET',
+ handlerCls: 'Next.route.doc'
+ },
+ {
+ route: '/doc',
+ method: 'POST',
+ handlerCls: 'Next.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'GET',
+ handlerCls: 'Next.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'PUT',
+ handlerCls: 'Next.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'DELETE',
+ handlerCls: 'Next.route.doc'
+ }
+];
+**/
View
@@ -1,138 +0,0 @@
-/**
- * This example demonstrates that used with a micro framework like Express
- * one can build quickly an API which model fits line by line
- * with the one used client-side
- *
- * @link Express Framework (https://github.com/visionmedia/express)
- */
-
-require.paths.unshift('/usr/local/lib/node_modules'); //specific to my dev environment
-try {
- var sencha = require('n-ext');
- sencha.bootstrapCore();
- var util = require('util');
- var express = require('express');
-} catch (e) {
- util.debug(e.toString());
- util.debug(util.inspect(e));
- process.exit();
-}
-
-Ext.require('Next.model.NoSQL.Document');
-var app = express.createServer();
-app.use(express.bodyParser());
-
-/**
- * List the items in the Doc collection
- */
-app.get('/doc/:start?/:limit?', function(req, res, next) {
- if (req.params.start && !req.params.limit) {
- next();
- return;
- }
- var store = Ext.create('Next.data.Store.Mongo', {
- autoLoad: {
- skip: req.params.start,
- limit: req.params.limit,
- callback: function(records, operation, success) {
- var _array = new Array();
- Ext.Array.forEach(records, function(item){
- var data = item.data;
- data[item.idProperty] = item.data._id.toString();
- _array.push(data);
- })
- res.json(_array);
- }
- }
- });
-});
-
-/**
- * Count the items in the Doc collection
- */
-app.get('/doc/count', function(req, res) {
- var store = Ext.create('Next.data.Store.Mongo');
- store.load(function() {
- res.send(store.getTotalCount().toString());
- });
-});
-
-/**
- * Creates an item
- */
-app.post('/doc', function(req, res) {
- var model = Ext.create('Next.model.NoSQL.Document');
- model.set(req.body);
- model.save({
- failure: function() {
- res.json({
- 'status': 'Unable to save the data'
- }, 500);
- },
- success: function() {
- res.json({'id' : this.get(this.idProperty.toString())});
- }
- });
-});
-
-
-/**
- * Retrieves an item by its id
- */
-app.get('/doc/:id', function(req, res) {
- var store = Ext.create('Next.data.Store.Mongo');
- store.getById(req.params.id, function(obj) {
- res.json(obj ? obj.data : null);
- });
-});
-
-/**
- * Updates an item by its id
- */
-app.put('/doc/:id', function(req, res) {
- var store = Ext.create('Next.data.Store.Mongo');
- store.getById(req.params.id, function(obj) {
- if (obj === null) {
- res.json({
- 'status': 'error',
- 'message' : 'No data with ID: ' + req.params.id
- }, 400);
- } else {
- var body = req.body;
- for (var idx in body) {
- obj.set(idx, body[idx]);
- }
- obj.save();
- res.send(obj.data);
- }
- });
-});
-
-/**
- * Deletes an item by its id
- */
-app.del('/doc/:id', function(req, res) {
- var store = Ext.create('Next.data.Store.Mongo');
-
- store.getById(req.params.id, function(obj) {
- if (obj === null) {
- res.json({
- 'status': 'error',
- 'message' : 'No data with ID: ' + req.params.id
- }, 400);
- } else {
- obj.destroy({
- success: function() {
- res.send(obj.data);
- },
- failure: function() {
- res.json({
- 'status': 'Unable to save the data'
- }, 500);
- },
- });
- }
- });
-});
-
-app.listen(3000);
View
@@ -0,0 +1,32 @@
+/**
+ * This example demonstrates that used with a micro framework like Express
+ * one can build quickly an API which model fits line by line
+ * with the one used client-side
+ *
+ * @link Express Framework (https://github.com/visionmedia/express)
+ */
+
+require.paths.unshift('/usr/local/lib/node_modules'); //specific to my dev environment
+try {
+ var util = require('util');
+ var sencha = require('n-ext');
+ sencha.bootstrapCore();
+ sencha.addLibPath(__dirname + '/lib/');
+ var express = require('express');
+} catch (e) {
+ util.debug(e.toString());
+ util.debug(e);
+ process.exit();
+}
+
+Ext.require('Next.model.NoSQL.Document');
+var app = express.createServer();
+app.use(express.bodyParser());
+
+var router = Ext.create('Next.router', {
+ path: __dirname + '/config/routes.json',
+ app: app
+});
+router.handle();
+
+app.listen(3000);
@@ -0,0 +1,32 @@
+exports.routes = [
+ {
+ route: '/doc/:start?/:limit?',
+ method: 'GET',
+ handler: {class: 'MyCRUD.route.doc', method: 'list'}
+ },
+ {
+ route: '/doc/count',
+ method: 'GET',
+ handler: {class: 'MyCRUD.route.doc', method: 'docCount'}
+ },
+ {
+ route: '/doc',
+ method: 'POST',
+ handlerCls: 'MyCRUD.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'GET',
+ handlerCls: 'MyCRUD.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'PUT',
+ handlerCls: 'MyCRUD.route.doc'
+ },
+ {
+ route: '/doc/:id',
+ method: 'DELETE',
+ handlerCls: 'MyCRUD.route.doc'
+ }
+];
@@ -0,0 +1,108 @@
+Ext.define('MyCRUD.route.doc', {
+ /**
+ * List the items in the Doc collection
+ */
+ list: function(req, res, next) {
+ if (req.params.start && !req.params.limit) {
+ next();
+ return;
+ }
+ var store = Ext.create('Next.data.Store.Mongo', {
+ autoLoad: {
+ skip: req.params.start,
+ limit: req.params.limit,
+ callback: function(records, operation, success) {
+ var _array = new Array();
+ Ext.Array.forEach(records, function(item){
+ var data = item.data;
+ data[item.idProperty] = item.data._id.toString();
+ _array.push(data);
+ })
+ res.json(_array);
+ }
+ }
+ });
+ },
+ /**
+ * Count the items in the Doc collection
+ */
+ docCount: function(req, res) {
+ var store = Ext.create('Next.data.Store.Mongo');
+ store.load(function() {
+ res.send(store.getTotalCount().toString());
+ });
+ },
+ /**
+ * Creates an item
+ */
+ post: function(req, res) {
+ var model = Ext.create('Next.model.NoSQL.Document');
+ model.set(req.body);
+ model.save({
+ failure: function() {
+ res.json({
+ 'status': 'Unable to save the data'
+ }, 500);
+ },
+ success: function() {
+ res.json({'id' : this.get(this.idProperty.toString())});
+ }
+ });
+ },
+ /**
+ * Retrieves an item by its id
+ */
+ get: function(req, res) {
+ var store = Ext.create('Next.data.Store.Mongo');
+ store.getById(req.params.id, function(obj) {
+ res.json(obj ? obj.data : null);
+ });
+ },
+ /**
+ * Updates an item by its id
+ */
+ put: function(req, res) {
+ var store = Ext.create('Next.data.Store.Mongo');
+ store.getById(req.params.id, function(obj) {
+ if (obj === null) {
+ res.json({
+ 'status': 'error',
+ 'message' : 'No data with ID: ' + req.params.id
+ }, 400);
+ } else {
+ var body = req.body;
+ for (var idx in body) {
+ obj.set(idx, body[idx]);
+ }
+ obj.save();
+ res.send(obj.data);
+ }
+ });
+ },
+ /**
+ * Deletes an item by its id
+ */
+ delete: function(req, res) {
+ var store = Ext.create('Next.data.Store.Mongo');
+
+ store.getById(req.params.id, function(obj) {
+ if (obj === null) {
+ res.json({
+ 'status': 'error',
+ 'message' : 'No data with ID: ' + req.params.id
+ }, 400);
+ } else {
+ obj.destroy({
+ success: function() {
+ res.send(obj.data);
+ },
+ failure: function() {
+ res.json({
+ 'status': 'Unable to save the data'
+ }, 500);
+ },
+ });
+ }
+ });
+ }
+});
Oops, something went wrong.

0 comments on commit 2ecb4c4

Please sign in to comment.