Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started to work on the router.

  • Loading branch information...
commit 2ecb4c417ee04a9ddbcbd5428552e1cea359345c 1 parent 812aa6b
@xcambar authored
View
1  .gitignore
@@ -1 +1,2 @@
lib/Ext
+lib-cov
View
39 config/routes.json
@@ -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
138 examples/CRUD.js
@@ -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
32 examples/CRUD/app.js
@@ -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);
View
32 examples/CRUD/config/routes.json
@@ -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'
+ }
+];
View
108 examples/CRUD/lib/MyCRUD/route/doc.js
@@ -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);
+ },
+ });
+ }
+ });
+ }
+});
View
38 lib/Next/router.js
@@ -0,0 +1,38 @@
+Ext.define('Next.router', {
+ app: null,
+ constructor: function(config) {
+ if (!config.app) {
+ Ext.Error.raise({
+ msg: 'no Application provided'
+ });
+ }
+ this.app = config.app;
+ if (!config.path) {
+ Ext.Error.raise({
+ msg: 'no route file specified'
+ });
+ }
+ this.routes = config.path;
+ },
+ handle: function() {
+ var me = this;
+ try {
+ var routeConfig = require(this.routes);
+ } catch (e) {
+ console.error(e);
+ Ext.Error.raise({
+ msg: e
+ });
+ };
+ Ext.Array.each(routeConfig.routes, function(item, index, all) {
+ var handler;
+ if (item.handlerCls) {
+ handler = Ext.create(item.handlerCls)[item.method];
+ } else {
+ var handlerConf = item.handler;
+ handler = Ext.create(handlerConf.class)[handlerConf.method];
+ }
+ me.app[Ext.util.Format.lowercase(item.method)].call(me.app, item.route, handler);
+ })
+ }
+})
View
31 tests/testNExt.js
@@ -1,41 +1,36 @@
+var assert = require('assert');
exports['Ext can be unset by the module'] = function(test){
- test.expect(1);
var sencha = require('n-ext');
sencha.bootstrapCore();
sencha.unset();
- test.ok(typeof(Ext) === 'undefined');
- test.done();
+ assert.throws(function() {
+ Ext; // Ext is not defined
+ });
+
};
exports['Ext can be unset and reset safely'] = function(test) {
- test.expect(3);
var sencha = require('n-ext');
sencha.bootstrapCore();
- test.ok(typeof(Ext) !== 'undefined');
+ assert.isDefined(Ext);
sencha.unset();
- test.ok(typeof(Ext) === 'undefined');
+ assert.throws(function() {
+ Ext; // Ext is not defined
+ });
sencha.bootstrapCore();
- test.ok(typeof(Ext) !== 'undefined', 'Global variable Ext should be defined');
- test.done();
+ assert.isDefined(Ext);
}
-/**
-**/
+
exports['Namespaces can not be loaded if path is undefined'] = function(test) {
- test.expect(1);
var sencha = require('n-ext');
sencha.bootstrapCore();
- test.throws(function() {
+ assert.throws(function() {
Ext.require('Test.Class');
});
sencha.unset();
- test.done();
}
exports['Ext is globally defined'] = function(test) {
- test.expect(1);
var sencha = require('n-ext');
sencha.bootstrapCore();
- test.ok(typeof(Ext) !== 'undefined', 'Global variable Ext should be defined');
- test.done();
+ assert.isDefined(Ext);
}
-/**
-**/
Please sign in to comment.
Something went wrong with that request. Please try again.