From 364943bfe08deebf60ff0266041497b69eff9481 Mon Sep 17 00:00:00 2001 From: Vladimir Polyakov Date: Mon, 4 May 2015 02:04:51 +0300 Subject: [PATCH] reflux draft --- bower.json | 3 +- gulpfile.js | 10 ++++ package.json | 2 + static/js/app/actions/project.js | 10 ++++ static/js/app/app.js | 49 ++++++++-------- static/js/app/components/app.jade | 6 ++ static/js/app/components/app.js | 22 +++++++ static/js/app/components/index.js | 11 ++++ static/js/app/components/projects/index.js | 11 ++++ static/js/app/components/projects/item.jade | 1 + static/js/app/components/projects/item.js | 15 +++++ static/js/app/components/projects/list.jade | 5 ++ static/js/app/components/projects/list.js | 32 ++++++++++ static/js/app/index.jade | 2 + static/js/app/resources.js | 15 +++++ static/js/app/stores/project.js | 24 ++++++++ static/js/main.js | 1 + static/js/templates/app/components/app.js | 33 +++++++++++ .../templates/app/components/projects/item.js | 33 +++++++++++ .../templates/app/components/projects/list.js | 58 +++++++++++++++++++ static/js/templates/app/index.js | 33 +++++++++++ views/index.jade | 2 + 22 files changed, 354 insertions(+), 24 deletions(-) create mode 100644 gulpfile.js create mode 100644 static/js/app/actions/project.js create mode 100644 static/js/app/components/app.jade create mode 100644 static/js/app/components/app.js create mode 100644 static/js/app/components/index.js create mode 100644 static/js/app/components/projects/index.js create mode 100644 static/js/app/components/projects/item.jade create mode 100644 static/js/app/components/projects/item.js create mode 100644 static/js/app/components/projects/list.jade create mode 100644 static/js/app/components/projects/list.js create mode 100644 static/js/app/index.jade create mode 100644 static/js/app/resources.js create mode 100644 static/js/app/stores/project.js create mode 100644 static/js/templates/app/components/app.js create mode 100644 static/js/templates/app/components/projects/item.js create mode 100644 static/js/templates/app/components/projects/list.js create mode 100644 static/js/templates/app/index.js diff --git a/bower.json b/bower.json index 8f69d71..ec6fdbb 100644 --- a/bower.json +++ b/bower.json @@ -8,7 +8,8 @@ "moment": "2.9.0", "react": "0.13.2", "requirejs": "2.1.17", - "jquery": "1.10.2" + "jquery": "1.10.2", + "reflux": "~0.2.7" }, "moduleType": [ "amd" diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..9072b90 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,10 @@ +'use strict'; + +var gulp = require('gulp'); +var gulpReactJade = require('gulp-react-jade-amd'); + +gulp.task('react-jade', function() { + return gulp.src('static/js/**/*.jade') + .pipe(gulpReactJade()) + .pipe(gulp.dest('static/js/templates')); +}); diff --git a/package.json b/package.json index f0d5154..b7d6b9f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,8 @@ "homepage": "https://github.com/okv/nci", "dependencies": { "data.io": "0.3.0", + "gulp": "^3.8.11", + "gulp-react-jade": "git://github.com/mizchi/gulp-react-jade", "jade": "1.9.2", "node-static": "0.7.6", "socket.io": "1.3.5", diff --git a/static/js/app/actions/project.js b/static/js/app/actions/project.js new file mode 100644 index 0000000..e61b2d5 --- /dev/null +++ b/static/js/app/actions/project.js @@ -0,0 +1,10 @@ +'use strict'; + +define(['reflux'], function(Reflux) { + var Actions = Reflux.createActions([ + 'load', + 'readAll' + ]); + + return Actions; +}); diff --git a/static/js/app/app.js b/static/js/app/app.js index 2007b32..c565143 100644 --- a/static/js/app/app.js +++ b/static/js/app/app.js @@ -1,33 +1,36 @@ 'use strict'; define([ - 'underscore', 'react', 'socketio', 'dataio', 'jquery' + 'react', 'templates/app/index', 'app/components/index', + 'app/actions/project' ], function( - _, React, socketio, dataio, $ + React, template, Components, ProjectActions ) { - var connect = dataio(socketio.connect()); + //var projectsTemplate = _($('#projects-template').html()).template(); + //$('#content').on('click', '.js-projects .js-run', function() { + //var projectName = $(this).parent('.js-project').data('name'); + //projects.sync('run', {projectName: projectName}, function(err, result) { + //$('#content').append( + //(err && err.message) + //); + //}); + //}); - var projects = connect.resource('projects'); - var builds = connect.resource('builds'); + //projects.sync('read', function(err, projects) { + //console.log('read complete'); + ////$('#content').html( + ////(err && err.message) || + ////projectsTemplate({projects: projects}) + ////); + //}); - var projectsTemplate = _($('#projects-template').html()).template(); - $('#content').on('click', '.js-projects .js-run', function() { - var projectName = $(this).parent('.js-project').data('name'); - projects.sync('run', {projectName: projectName}, function(err, result) { - $('#content').append( - (err && err.message) - ); - }); - }); + //builds.subscribe(function(data, action) { + //$('#content').append(action.action + ': ' + JSON.stringify(data)); + //}); - projects.sync('read', function(err, projects) { - $('#content').html( - (err && err.message) || - projectsTemplate({projects: projects}) - ); - }); + React.render(template({ + App: Components.App + }), document.getElementById('react-content')); - builds.subscribe(function(data, action) { - $('#content').append(action.action + ': ' + JSON.stringify(data)); - }); + ProjectActions.readAll(); }); diff --git a/static/js/app/components/app.jade b/static/js/app/components/app.jade new file mode 100644 index 0000000..50d6c32 --- /dev/null +++ b/static/js/app/components/app.jade @@ -0,0 +1,6 @@ +div + h1 application + + ProjectsList() + + diff --git a/static/js/app/components/app.js b/static/js/app/components/app.js new file mode 100644 index 0000000..1ef5ff7 --- /dev/null +++ b/static/js/app/components/app.js @@ -0,0 +1,22 @@ +'use strict'; + +define([ + 'react', + 'app/components/projects/index', + 'templates/app/components/app' +], function(React, Projects, template) { + var Component = React.createClass({ + getInitialState: function() { + return { + projects: [] + }; + }, + render: function() { + return template({ + ProjectsList: Projects.List + }); + } + }); + + return Component; +}); diff --git a/static/js/app/components/index.js b/static/js/app/components/index.js new file mode 100644 index 0000000..d96b3be --- /dev/null +++ b/static/js/app/components/index.js @@ -0,0 +1,11 @@ +'use strict'; + +define([ + 'app/components/projects/index', + 'app/components/app', +], function(ProjectsComponents, App) { + return { + App: App, + ProjectsComponents: ProjectsComponents + }; +}); diff --git a/static/js/app/components/projects/index.js b/static/js/app/components/projects/index.js new file mode 100644 index 0000000..1cd9ed4 --- /dev/null +++ b/static/js/app/components/projects/index.js @@ -0,0 +1,11 @@ +'use strict'; + +define([ + 'app/components/projects/item', + 'app/components/projects/list' +], function(Item, List) { + return { + Item: Item, + List: List + }; +}); diff --git a/static/js/app/components/projects/item.jade b/static/js/app/components/projects/item.jade new file mode 100644 index 0000000..8baa4b3 --- /dev/null +++ b/static/js/app/components/projects/item.jade @@ -0,0 +1 @@ +h1= item.name diff --git a/static/js/app/components/projects/item.js b/static/js/app/components/projects/item.js new file mode 100644 index 0000000..c98d318 --- /dev/null +++ b/static/js/app/components/projects/item.js @@ -0,0 +1,15 @@ +'use strict'; + +define([ + 'react', 'templates/app/components/projects/item' +], function(React, template) { + var Component = React.createClass({ + render: function() { + return template({ + item: this.props.item + }); + } + }); + + return Component; +}); diff --git a/static/js/app/components/projects/list.jade b/static/js/app/components/projects/list.jade new file mode 100644 index 0000000..34f95b3 --- /dev/null +++ b/static/js/app/components/projects/list.jade @@ -0,0 +1,5 @@ +div + h2 projects list + - console.log(this); + each project in items + Item(item=project) diff --git a/static/js/app/components/projects/list.js b/static/js/app/components/projects/list.js new file mode 100644 index 0000000..91f00f4 --- /dev/null +++ b/static/js/app/components/projects/list.js @@ -0,0 +1,32 @@ +'use strict'; + +define([ + 'react', + 'reflux', + './item', + 'app/stores/project', + 'templates/app/components/projects/list' +], function(React, Reflux, Item, projectStore, template) { + var Component = React.createClass({ + mixins: [Reflux.ListenerMixin], + componentDidMount: function() { + this.listenTo(projectStore, this.updateItems); + }, + updateItems: function(items) { + this.setState({items: items}); + }, + render: function() { + return template({ + Item: Item, + items: this.state.items + }); + }, + getInitialState: function() { + return { + items: [] + }; + } + }); + + return Component; +}); diff --git a/static/js/app/index.jade b/static/js/app/index.jade new file mode 100644 index 0000000..e45e67d --- /dev/null +++ b/static/js/app/index.jade @@ -0,0 +1,2 @@ +div + App() diff --git a/static/js/app/resources.js b/static/js/app/resources.js new file mode 100644 index 0000000..1f45c29 --- /dev/null +++ b/static/js/app/resources.js @@ -0,0 +1,15 @@ +'use strict'; + +define([ + 'socketio', 'dataio' +], function(socketio, dataio) { + var connect = dataio(socketio.connect()); + + var projects = connect.resource('projects'); + var builds = connect.resource('builds'); + + return { + projects: projects, + builds: builds + } +}); diff --git a/static/js/app/stores/project.js b/static/js/app/stores/project.js new file mode 100644 index 0000000..9649517 --- /dev/null +++ b/static/js/app/stores/project.js @@ -0,0 +1,24 @@ +'use strict'; + +define([ + 'reflux', 'app/actions/project', 'app/resources' +], function(Reflux, ProjectActions, resources) { + var Store = Reflux.createStore({ + init: function() { + this.listenTo(ProjectActions.load, this.load); + this.listenTo(ProjectActions.readAll, this.readAll); + }, + load: function(items) { + this.trigger(items); + }, + readAll: function() { + resources.projects.sync('read', function(err, projects) { + console.log(err); + console.log(projects); + ProjectActions.load(projects) + }); + } + }); + + return Store; +}); diff --git a/static/js/main.js b/static/js/main.js index 8629062..dcc88d0 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -5,6 +5,7 @@ require.config({ paths: { underscore: 'libs/underscore/underscore', react: 'libs/react/react-with-addons', + reflux: 'libs/reflux/dist/reflux', dataio: '/data.io', socketio: '/socket.io/socket.io.js', jquery: 'libs/jquery/jquery' diff --git a/static/js/templates/app/components/app.js b/static/js/templates/app/components/app.js new file mode 100644 index 0000000..cc6d617 --- /dev/null +++ b/static/js/templates/app/components/app.js @@ -0,0 +1,33 @@ +define(['react'], function(React) {return (function (React) { + var fn = function(locals) { + var tags = []; + var locals_for_with = locals || {}; + (function(ProjectsList) { + tags.push(React.createElement("div", {}, React.createElement("h1", {}, "application"), React.createElement(ProjectsList, {}))); + }).call(this, "ProjectsList" in locals_for_with ? locals_for_with.ProjectsList : typeof ProjectsList !== "undefined" ? ProjectsList : undefined); + if (tags.length === 1 && !Array.isArray(tags[0])) { + return tags.pop(); + } + tags.unshift("div", null); + return React.createElement.apply(React, tags); + }; + + fn.locals = function setLocals(locals) { + var render = this; + function newRender(additionalLocals) { + var newLocals = {}; + for (var key in locals) { + newLocals[key] = locals[key]; + } + if (additionalLocals) { + for (var key in additionalLocals) { + newLocals[key] = additionalLocals[key]; + } + } + return render.call(this, newLocals); + } + newRender.locals = setLocals; + return newRender; + };; + return fn; +}(typeof React !== "undefined" ? React : require(".//Users/vladimir/projects/nci/node_modules/gulp-react-jade-amd/node_modules/react/react.js")));}); \ No newline at end of file diff --git a/static/js/templates/app/components/projects/item.js b/static/js/templates/app/components/projects/item.js new file mode 100644 index 0000000..195a21e --- /dev/null +++ b/static/js/templates/app/components/projects/item.js @@ -0,0 +1,33 @@ +define(['react'], function(React) {return (function (React) { + var fn = function(locals) { + var tags = []; + var locals_for_with = locals || {}; + (function(item) { + tags.push(React.createElement("h1", {}, item.name)); + }).call(this, "item" in locals_for_with ? locals_for_with.item : typeof item !== "undefined" ? item : undefined); + if (tags.length === 1 && !Array.isArray(tags[0])) { + return tags.pop(); + } + tags.unshift("div", null); + return React.createElement.apply(React, tags); + }; + + fn.locals = function setLocals(locals) { + var render = this; + function newRender(additionalLocals) { + var newLocals = {}; + for (var key in locals) { + newLocals[key] = locals[key]; + } + if (additionalLocals) { + for (var key in additionalLocals) { + newLocals[key] = additionalLocals[key]; + } + } + return render.call(this, newLocals); + } + newRender.locals = setLocals; + return newRender; + };; + return fn; +}(typeof React !== "undefined" ? React : require(".//Users/vladimir/projects/nci/node_modules/gulp-react-jade-amd/node_modules/react/react.js")));}); \ No newline at end of file diff --git a/static/js/templates/app/components/projects/list.js b/static/js/templates/app/components/projects/list.js new file mode 100644 index 0000000..b78c1bf --- /dev/null +++ b/static/js/templates/app/components/projects/list.js @@ -0,0 +1,58 @@ +define(['react'], function(React) {return (function (React) { + var fn = function(locals) { + var tags = []; + var locals_for_with = locals || {}; + (function(Item, console, items) { + tags.push(React.createElement.apply(React, [ "div", {} ].concat(function() { + var tags = []; + tags.push(React.createElement("h2", {}, "projects list")); + console.log(this); + tags.push(function() { + var tags = []; + var $$obj = items; + if ("number" == typeof $$obj.length) for (var $index = 0, $$l = $$obj.length; $$l > $index; $index++) { + var project = $$obj[$index]; + tags.push(React.createElement(Item, { + item: project + })); + } else { + var $$l = 0; + for (var $index in $$obj) { + $$l++; + var project = $$obj[$index]; + tags.push(React.createElement(Item, { + item: project + })); + } + } + return tags; + }.call(this)); + return tags; + }.call(this)))); + }).call(this, "Item" in locals_for_with ? locals_for_with.Item : typeof Item !== "undefined" ? Item : undefined, "console" in locals_for_with ? locals_for_with.console : typeof console !== "undefined" ? console : undefined, "items" in locals_for_with ? locals_for_with.items : typeof items !== "undefined" ? items : undefined); + if (tags.length === 1 && !Array.isArray(tags[0])) { + return tags.pop(); + } + tags.unshift("div", null); + return React.createElement.apply(React, tags); + }; + + fn.locals = function setLocals(locals) { + var render = this; + function newRender(additionalLocals) { + var newLocals = {}; + for (var key in locals) { + newLocals[key] = locals[key]; + } + if (additionalLocals) { + for (var key in additionalLocals) { + newLocals[key] = additionalLocals[key]; + } + } + return render.call(this, newLocals); + } + newRender.locals = setLocals; + return newRender; + };; + return fn; +}(typeof React !== "undefined" ? React : require(".//Users/vladimir/projects/nci/node_modules/gulp-react-jade-amd/node_modules/react/react.js")));}); \ No newline at end of file diff --git a/static/js/templates/app/index.js b/static/js/templates/app/index.js new file mode 100644 index 0000000..e2e8b3e --- /dev/null +++ b/static/js/templates/app/index.js @@ -0,0 +1,33 @@ +define(['react'], function(React) {return (function (React) { + var fn = function(locals) { + var tags = []; + var locals_for_with = locals || {}; + (function(App) { + tags.push(React.createElement("div", {}, React.createElement(App, {}))); + }).call(this, "App" in locals_for_with ? locals_for_with.App : typeof App !== "undefined" ? App : undefined); + if (tags.length === 1 && !Array.isArray(tags[0])) { + return tags.pop(); + } + tags.unshift("div", null); + return React.createElement.apply(React, tags); + }; + + fn.locals = function setLocals(locals) { + var render = this; + function newRender(additionalLocals) { + var newLocals = {}; + for (var key in locals) { + newLocals[key] = locals[key]; + } + if (additionalLocals) { + for (var key in additionalLocals) { + newLocals[key] = additionalLocals[key]; + } + } + return render.call(this, newLocals); + } + newRender.locals = setLocals; + return newRender; + };; + return fn; +}(typeof React !== "undefined" ? React : require(".//Users/vladimir/projects/nci/node_modules/gulp-react-jade-amd/node_modules/react/react.js")));}); \ No newline at end of file diff --git a/views/index.jade b/views/index.jade index 081c93f..b75945a 100644 --- a/views/index.jade +++ b/views/index.jade @@ -22,3 +22,5 @@ html #content + + #react-content